TThread Lock and UnLock

Hello guys,

I am using TThread within my code to create several thread in parallel. For this I have to use ROOT 5.34.36
I know already the thread.C available in the tutoriel, BUT I maybe need more detail about them.

So far, it sounds like you have to lock your thread when you are acting on a global variable or using a static method which use common variable (e.g. TFile::Open)

  1. First case, I am writing in N files which are all independants (1file = 1 thread). In this case for example, shoud I lock the thread before writing. The same question when closing?

  2. When I am creating a TNamed object, or deleting should I lock also ? (TH1D::TH1D(); TEntryList; TTreeFormula, etc…)

  3. In order to balance the usage of memory, I implemented a counter which is increased when a thread start and decrease at the end of the thread. Is there a better way to balance the load ? I heart about a threadpool; is it made for this ?

Thanks you !

Hi Marco,

the threading and resources’ protection mechanisms are mature in the latest releases of the ROOT6 cycle (see … icore.html). These releases, for this and other reasons, should be seriously considered.
How much bound are you to ROOT5?


Hi Danilo,

I see, I agree. That’s what I thought… I noticed some very interesting features like :

I will think about upgrade to those release. My problem is that my dependancies don’t like g++ 64-bits flag enabled. I tried to play and see, sending some jobs on lxbatch. This is still not clear to me when I should lock or not, but it looks like I found a good compromise at this moment.

In case someone know about the restrictions for Root5 that would be wonderful.


(Even in v5,) TFile::Open and deletion when being apply to your use case (one TFile per thread) does not need to be globally locked. The touched global variables (gFile and gDirectory) are thread local when thread support is enable. Thread support is enabled automatically once you create a TThread object. (Nonetheless, since we have stopped updating v5 a while back, v6 is much more stable when using in multi-thread mode).

You may want to look at the class TThreadPool.



are you aware of the lcg releases? These are very rich stacks of software, built coherently and available on CVMFS (and therefore on the cern batch).
Configuring the environment is straightforward (for example, /cvmfs/, here for the catalogue of the packages - ROOT is of course there together with many other scientific packages).