How do you implement While-loops in multithreaded g4

Hello everyone!

I want to implement an event loop which is conditional: The loop can stop when a certain condition is met. Eg, reaching a predefined limit in total energy deposition, or reaching a certain precision, etc. Thsi is a quantity that can be accumulated in a G4Accumulable or G4StatAnalysis obj.

Now, in sequential mode that can be easily implemented in EndofEvent: you just calculate your quantity there, accumulate it, and either break the loop and stop the simulation if the condition has been reached, or do nothing (ie continue with the event loop).

Obviously this is not possible in multithreaded mode.
(Well, sure, one could apply one mutex per event but that doesn’t sound very clever…)
Then, applying a mutex per few events, merging all accumulables, and comparing the merged output w/ the condition would be a solution, but then that is not flexible enough. If one applies the mutex every n events, n could prove to be too small (eg for events with small energies) or huge (for events with large energies etc.)

Is there some “standard” way to do similar things? And if this is the case, could someone point me to it, since I haven’t been able to find it?

Thanks,
Dimitris