I was just wondering if histograms in ROOT are threadsafe. For example TH1D, I tried filling a histogram by multiple threads and it seems to be working fine. However, I found this tutorial which says that I must use TThreadedObject in order to use the histograms with multiple threads.
Would you please elaborate on the thread-safety of classes like TH1D, TH2D and TProfile2D?
Hi, TH1D, TH2D and TProfile2D are not thread-aware. Most const methods should be safe to be called concurrently, but no guarantees are given.
TThreadedObject is a helper class that transparently creates one copy of the object (an histogram, in this case) per thread and merges the thread-local copies at the end. Filling different histograms in different threads is safe, and constructing histograms concurrently is safe if ROOT::EnableThreadSafety() has been called.
Thanks for your quick response. Just to make sure I understood you correctly, you mean that both calling ROOT::EnableThreadSafety()and using TThreadedObject only guarantees thread-safety?
To safely create and fill ROOT histograms concurrently from multiple threads, you need both ROOT::EnableThreadSafety (which makes ROOT global lists as well as ROOT memory management thread-aware and adds some locks around global lists modification) and TThreadedObject (or equivalent logic that fills one histogram per thread with same binning and merges them at the end).