From caf94305c9344a6debc14746b92c40658f8a5e9c Mon Sep 17 00:00:00 2001 From: Christopher Jones Date: Tue, 12 May 2015 22:49:23 +0200 Subject: [PATCH] Made statics in NuclearInteractionFTFSimulator thread safe The static analyzer identified a thread-safety issue with the static class members of NuclearInteractionFTFSimulator. Using an std::call_once makes sure the array is safely initialized. --- .../src/NuclearInteractionFTFSimulator.cc | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/FastSimulation/MaterialEffects/src/NuclearInteractionFTFSimulator.cc b/FastSimulation/MaterialEffects/src/NuclearInteractionFTFSimulator.cc index be4b6cf0cbd6b..1a152f91e1c89 100644 --- a/FastSimulation/MaterialEffects/src/NuclearInteractionFTFSimulator.cc +++ b/FastSimulation/MaterialEffects/src/NuclearInteractionFTFSimulator.cc @@ -1,3 +1,6 @@ +// system headers +#include + // Framework Headers #include "FWCore/MessageLogger/interface/MessageLogger.h" @@ -61,8 +64,9 @@ #include "G4PhysicsLogVector.hh" #include "G4SystemOfUnits.hh" -const G4ParticleDefinition* NuclearInteractionFTFSimulator::theG4Hadron[] = {0}; -int NuclearInteractionFTFSimulator::theId[] = {0}; +static std::once_flag initializeOnce; +[[cms::thread_guard("initializeOnce")]] const G4ParticleDefinition* NuclearInteractionFTFSimulator::theG4Hadron[] = {0}; +[[cms::thread_guard("initializeOnce")]] int NuclearInteractionFTFSimulator::theId[] = {0}; const double fact = 1.0/CLHEP::GeV; @@ -133,7 +137,7 @@ NuclearInteractionFTFSimulator::NuclearInteractionFTFSimulator( theBertiniCascade = new G4CascadeInterface(); // Geant4 particles and cross sections - if(!theG4Hadron[0]) { + std::call_once(initializeOnce, [this] () { theG4Hadron[0] = G4Proton::Proton(); theG4Hadron[1] = G4Neutron::Neutron(); theG4Hadron[2] = G4PionPlus::PionPlus(); @@ -177,7 +181,7 @@ NuclearInteractionFTFSimulator::NuclearInteractionFTFSimulator( for(int i=0; iGetPDGEncoding(); } - } + }); // local objects vect = new G4PhysicsLogVector(npoints-1,100*MeV,TeV);