Multi-thread problems

Please fill out the following information to help in answering your question, and also see tips for posting code snippets. If you don’t provide this information it will take more time to help with your problem!

_Geant4 Version:_11.2.1
_Operating System:_GNU ld (GNU Binutils) 2.37
_Compiler/Version:_11.2.0
_CMake Version:_GNU Make 4.3


Hi, I have a program that needs to use multithreading and merge the results. I am interested in how multithreading works in Geant4. I understand that it is divided into main thread and worker thread. Is the main thread the last to run.cc or runaction.cc? If so, for example, if I want to simulate 100 million particles, will the main thread simulate some of the particles and produce results or will all the particles be simulated by the worker thread? Do I need to consider the simulation values ​​in the main thread when merging?

You may find the discussion of multithreading from the Toolkit Developers Guide useful (Parallelism in Geant4: multi-threading capabilities — Book For Toolkit Developers 11.2 documentation).

The framework has been developed so that application users do not need to know about multithreading, so long as they follow the guidance on how to register processes and run events.

If you make use of G4Analysis, then you should not need to do any “merging” on your own – following the coding instructions in the documentation will ensure that the G4Analysis manager code can do the right thing automatically. If you are using ROOT directly (TFile, TTree, TBranch), then you should definitely read the Toolkit documentation and make your code thread-safe.

Thanks !I will learn this discussion.