Message: G4Cache vs static G4ThreadLocal when there is only one class instance Not Logged In (login)
 Next-in-Thread Next-in-Thread
 Next-in-Forum Next-in-Forum

None G4Cache vs static G4ThreadLocal when there is only one class instance 

Forum: Multithreading
Date: 15 Aug, 2016
From: <herr_apa>

Hi,

Among the field examples in examples/extended/field/, there are two different approaches to making the field thread-safe. In the field01 to field04 examples, the DetectorConstruction has a FieldSetup class wrapped in a G4Cache (e.g. G4Cache<F01FieldSetup*>). The FieldSetup class, which is instantiated in ConstructSDandField, takes care of creating a G4ChordFinder and registering it with the FieldManager. In the field05 and field06 examples, the DetectorConstruction instead has a static G4ThreadLocal G4Field*. The G4ChordFinder is created directly in ConstructSDandField.

I don't understand the motivation (if there is any) for choosing the G4Cache approach in one case and the static G4ThreadLocal approach in another. I think I can summarize my confusion in two questions.

1) It is my understanding that the master thread only instantiates one DetectorConstruction. Using the notation from the Twiki (https://twiki.cern.ch/twiki/bin/view/Geant4/Geant4MTForKernelDevelopers), case F and case H then become degenerate. Does that mean G4Cache and static G4ThreadLocal would both make the code thread-safe?

2) Let's take an example. In field01 the code looks like this:

G4Cache<F01FieldSetup*> fEmFieldSetup; //In F01DetectorConstruction.hh

if(!fEmFieldSetup.Get()) { //In F01DetectorConstruction::ConstructSDandField
  F01FieldSetup* fieldSetup = new F01FieldSetup(G4ThreeVector( 3.3*tesla, 0.0, 0.0 ) );
  G4AutoDelete::Register(fieldSetup); // Kernel will delete the F01FieldSetup
  fEmFieldSetup.Put(fieldSetup);
}

Say I change the code into this:

static G4ThreadLocal F01FieldSetup* fEmFieldSetup; //In F01DetectorConstruction.hh

G4ThreadLocal F01FieldSetup* F01DetectorConstruction::fEmFieldSetup = 0; //In F01DetectorConstruction.cc

if(!fEmFieldSetup) { //In F01DetectorConstruction::ConstructSDandField
  fieldSetup = new F01FieldSetup(G4ThreeVector( 3.3*tesla, 0.0, 0.0 ) );
}

Would the latter approach be equally valid? Are there any reasons for choosing one over the other. The TWiki seems to imply that G4Cache comes with a performance overhead.

Thanks!

// Anders

Inline Depth:
 1 1
 All All
Outline Depth:
 1 1
 2 2
 All All
Add message: (add)

1 Feedback: Re: G4Cache vs static G4ThreadLocal when there is only one class instance   (Andrea Dotti - 15 Aug, 2016)
 Add Message Add Message
to: "G4Cache vs static G4ThreadLocal when there is only one class instance"

 Subscribe Subscribe

This site runs SLAC HyperNews version 1.11-slac-98, derived from the original HyperNews