diff --git a/Simulation/G4Atlas/G4AtlasTools/src/G4ThreadInitTool.cxx b/Simulation/G4Atlas/G4AtlasTools/src/G4ThreadInitTool.cxx index e7306476e847f1b739465c908372ff99eaff1490..c9716c25046aebab2584af83689986a4c3f929c3 100644 --- a/Simulation/G4Atlas/G4AtlasTools/src/G4ThreadInitTool.cxx +++ b/Simulation/G4Atlas/G4AtlasTools/src/G4ThreadInitTool.cxx @@ -114,12 +114,23 @@ void G4ThreadInitTool::initThread() //----------------------------------------------------------------------------- void G4ThreadInitTool::terminateThread() { - ATH_MSG_INFO("terminateThread ==> tbb thread 0x" << + ATH_MSG_DEBUG("terminateThread ==> tbb thread 0x" << std::hex << pthread_self() << std::dec); // Geant4 worker finalization - G4RunManager::GetRunManager()->RunTermination(); - - // Atomic decrement number of initialized threads - m_nInitThreads--; + auto runMgr = G4RunManager::GetRunManager(); + ATH_MSG_DEBUG("G4RunManager ptr" << runMgr); + if(runMgr) { + runMgr->RunTermination(); + ATH_MSG_INFO("terminateThread ==> safely called G4AtlasWorkerRunManager::RunTermination for tbb thread 0x" << + std::hex << pthread_self() << std::dec); + // Atomic decrement number of initialized threads + m_nInitThreads--; + } + else { + ATH_MSG_WARNING("skipping attempt to call terminateThread for tbb thread 0x" << + std::hex << pthread_self() << std::dec << + " without having first called initThread."); + // Not decrementing m_nInitThreads as initThread was not called in this case. + } }