Fill histogram in parallel with a multithreaded approach using TThreadedObject and TThreadedObject::SnapshotMerge.
The difference with the multiprocessing case, see mp_parallelHistoFill, is that we cannot count on the copy-on-write mechanism here. Instead, we need to protect the histogram resource with a TThreadedObject class. The result of the filling is monitored with the SnapshotMerge method. This method is not thread safe: in the presence of ROOT histograms, the system will not crash but the result is not uniquely defined.
Int_t mt_parallelHistoFill()
{
auto fillRandomHisto = [&](int seed = 0) {
auto histogram = ts_h.Get();
histogram->Fill(rndm.Gaus(0, 1));
}
};
std::vector<std::thread> pool;
auto monitor = [&]() {
std::this_thread::sleep_for(std::chrono::duration<double, std::nano>(500));
auto h = ts_h.SnapshotMerge();
std::cout <<
"Entries for the snapshot " <<
h->GetEntries() << std::endl;
}
};
pool.emplace_back(monitor);
pool.emplace_back(fillRandomHisto, seed);
}
for (auto &&t : pool)
t.join();
auto sumRandomHisto = ts_h.Merge();
std::cout << "Entries for the total sum " << sumRandomHisto->GetEntries() << std::endl;
sumRandomHisto->DrawClone();
return 0;
}
int Int_t
Signed integer 4 bytes (int).
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int).
A wrapper to make object instances thread private, lazily.
Random number generator class based on M.
void EnableThreadSafety()
Enable support for multi-threading within the ROOT code in particular, enables the global mutex to ma...
- Date
- January 2016
- Author
- Danilo Piparo
Definition in file mt_parallelHistoFill.C.