Multithreading ROOT project

Hi, I’m new to this forum so I didn’t know where to post this.
I’m working on a C++ binary which creates and fills multiple TH1F histograms with a huge amount of entries. To speed things up, I decided to try multithreading the program using the TThreadedObject class and wrapping my histograms in it.
I just can’t work out whether my project needs the following two lines:
ROOT::EnableImplicitMT(NTasks); ROOT::EnableThreadSafety();
Here NTasks is the number of threads I want to launch.

Also, is it a good idea to use TThreadedObject? Or is it better to use plain standard library threads and mutexes?

1 Like

Hi @fresh,
welcome to the ROOT forum!

ROOT::EnableImplicitMT is required to work with ROOT objects in a multi-thread application. It implies ROOT::EnableThreadSafety, as per the docs.

If what you would do is to fill a different TH1F per thread and then merge them at the end, TThreadedObject is a good idea – that’s exactly what it’s for, and you don’t have to worry about the gory details.

Cheers,
Enrico