Is ROOT threadsafe?

From: Konstantin Olchanski <olchansk_at_triumf.ca>
Date: Tue, 1 Feb 2011 09:14:49 -0800

Hi, rooters - what is the latest situation with thread safety in ROOT?

I have an application that uses a separate thread to grab ROOT objects created by end users (TH1 & ilk), serialize them and send them over a TCP connection to a different application (happily single-threaded, where users look at these objects).

It's a poor man's substitute for PAW/HBOOK shared memory histograms and it is used mainly to look at live data produced by the MIDAS data acquisition system. http://midas.psi.ch
http://daq-plone.triumf.ca/SR/rootana

All this was written some years ago, the ROOT world has moved on since then and now time has come to renew my code and I am not sure if my thread implementation is correct for the latest versions of ROOT.

So I look at the documentation and do not see happy results:

  1. user guide v5.26 introduces TThread::Lock and Unlock, but does not spell out which ROOT operations have to be locked and which are thread safe. The text also says that "parts of ROOT are not thread safe yet" (search for word "yet"). This implies that some unspecified parts of ROOT are thread-safe after all and promises that other parts will become thread-safe. I cannot tell if this text is up to date, so what's the current situation?
  2. tutorials/thread/threadsh1.C is one of the few examples of using threads and it uses TThread::Lock inconsistently. For example it does not lock calls to gRandom - is that one part of ROOT that is thread safe? Also in the sub-thread, calls to canvas->cd() and th1->Draw() are locked, but canvas->Modified() is not; and the main thread does not seem to lock anything at all. If this example is correct at all, it implies that some implicit locking is happening somewhere else?

So what looking do I need to do in my thread?

I use things like tobject->InheritsFrom(), gRoot->FindObjectAny(), th1->Reset(), tdirectory->GetList(), new TKey(...), tobject->GetName(), TMessage, TList, TSocket (TSocket I believe is thread-safe), tdirectory->GetListOfKeys(), tkey->FindObject(), tfolder->GetListOfFolders(), etc

So what's thread safe and what is not? (Plus remember that the end user can create and destroy objects all the time at any time - my thread has to be protected against race conditions on access to destroyed objects (i.e. th1=FindObject(); /* object is deleted by user in the main thread */; th1->Reset(); /* boom!!! */).

For the curious, the actual code is here: http://ladd00.triumf.ca/viewvc/rootana/trunk/libNetDirectory/netDirectoryServer.cxx?view=markup it uses a mickey-mouse locking scheme in RootLock.cxx that does not use TThread::Lock (probably did not exist when my code was written).

-- 
Konstantin Olchanski
Data Acquisition Systems: The Bytes Must Flow!
Email: olchansk-at-triumf-dot-ca
Snail mail: 4004 Wesbrook Mall, TRIUMF, Vancouver, B.C., V6T 2A3, Canada
Received on Tue Feb 01 2011 - 18:14:56 CET

This archive was generated by hypermail 2.2.0 : Wed Feb 02 2011 - 11:50:01 CET