Skip to content

Commit

Permalink
Made statics in NuclearInteractionFTFSimulator thread safe
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Dr15Jones committed May 12, 2015
1 parent afa031e commit caf9430
Showing 1 changed file with 8 additions and 4 deletions.
@@ -1,3 +1,6 @@
// system headers
#include <mutex>

// Framework Headers
#include "FWCore/MessageLogger/interface/MessageLogger.h"

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -177,7 +181,7 @@ NuclearInteractionFTFSimulator::NuclearInteractionFTFSimulator(
for(int i=0; i<numHadrons; ++i) {
theId[i] = theG4Hadron[i]->GetPDGEncoding();
}
}
});

// local objects
vect = new G4PhysicsLogVector(npoints-1,100*MeV,TeV);
Expand Down

0 comments on commit caf9430

Please sign in to comment.