# Need advice on G4AutoLock in primary generator

**URL:** <https://geant4-forum.web.cern.ch/t/need-advice-on-g4autolock-in-primary-generator/15421>\
**Category:** Particles, Track, Event, Run and Biasing\
**Created:** [November 3, 2026, 4:57am UTC](https://geant4-forum.web.cern.ch/t/need-advice-on-g4autolock-in-primary-generator/15421 "2026-11-03T04:57:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vyacheslav\_Porosev](https://geant4-forum.web.cern.ch/user_avatar/geant4-forum.web.cern.ch/vyacheslav_porosev/32/5070_2.png) [@Vyacheslav\_Porosev](https://geant4-forum.web.cern.ch/u/Vyacheslav_Porosev)\
**Post date:** [November 3, 2026, 4:57am UTC](https://geant4-forum.web.cern.ch/t/need-advice-on-g4autolock-in-primary-generator/15421/1 "2026-11-03T04:57:49Z")

</div>

Please fill out the following information to help in answering your question, and also see [tips for posting code snippets](https://geant4-forum.web.cern.ch/t/how-to-post-code-snippets/9944). If you don’t provide this information it will take more time to help with your problem!

_Geant4 Version:_  
_Operating System:_  
_Compiler/Version:_  
_CMake Version:_

* * *

Hello all,

I am sorry, but I could not find clear answer on my question:

If I should to use G4AutoLock feature in primary generator action in a multi-threaded application when I use the particle energy spectrum from file? Or it will be unsafe without it?

Many thanks in advance,

Vyacheslav

P.S. Example of the code…

G4Mutex myPrimGenMutex = G4MUTEX\_INITIALIZER;

PrimaryGeneratorAction::PrimaryGeneratorAction() : G4VUserPrimaryGeneratorAction()  
{  
G4AutoLock lock(&myPrimGenMutex);

// default particle kinematic  
G4ParticleTable\* particleTable = G4ParticleTable::GetParticleTable();  
G4ParticleDefinition\* electronDef = particleTable-\>FindParticle(“e-”); //gamma, e-, neutron, proton, opticalphoton  
//  
fGPS = new G4GeneralParticleSource();

…

fGPS-\>GetCurrentSource()-\>SetParticleDefinition(electronDef);

//// set energy distribution  
G4SPSEneDistribution\* electronEnergy = fGPS-\>GetCurrentSource()-\>GetEneDist();  
electronEnergy-\>SetEnergyDisType(“Arb”);  
//Electron spectrum  
electronEnergy-\>ArbEnergyHistoFile(“Spectrum1.txt”);  
//  
//The only valid arguments are Lin, Log, Exp or Spline.  
electronEnergy-\>ArbInterpolate(“Lin”);

…

}

PrimaryGeneratorAction::~PrimaryGeneratorAction()  
{  
G4AutoLock lock(&myPrimGenMutex);  
delete fGPS;  
}

void PrimaryGeneratorAction::GeneratePrimaries(G4Event\* anEvent)  
{  
G4AutoLock lock(&myPrimGenMutex);

//Generate primary particle  
fGPS-\>GeneratePrimaryVertex(anEvent);

}

---

<div class="post-metadata">

**Author:** ![mkelsey](https://geant4-forum.web.cern.ch/user_avatar/geant4-forum.web.cern.ch/mkelsey/32/49_2.png) [@mkelsey](https://geant4-forum.web.cern.ch/u/mkelsey)\
**Post date:** [November 4, 2026, 12:05am UTC](https://geant4-forum.web.cern.ch/t/need-advice-on-g4autolock-in-primary-generator/15421/2 "2026-11-04T00:05:03Z")

</div>

It appears that you are reading in the energy spectrum in the constructor, not event by event. In that case, you do not need a mutex. Each thread can open the file and read it through their own, local stream object.

Similarly, I don’t know why you’d need a mutex in `GeneratePrimaries()`. All you’re doing is calling through to GPS, and your class has its own local instance of GPS, so there’s no chance of a thread collision.

In the CDMS simulation framework, we read _per event_ information from an input text file. There, we _do_ use a mutex, with a global singleton that opens the file only one time, and reads events when called by the workers. That ensures that each event is fed to just one thread, and we don’t have duplications. But that doesn’t seem to be what you’re doing here.

---

<div class="post-metadata">

**Author:** ![Vyacheslav\_Porosev](https://geant4-forum.web.cern.ch/user_avatar/geant4-forum.web.cern.ch/vyacheslav_porosev/32/5070_2.png) [@Vyacheslav\_Porosev](https://geant4-forum.web.cern.ch/u/Vyacheslav_Porosev)\
**Post date:** [November 4, 2026, 8:18am UTC](https://geant4-forum.web.cern.ch/t/need-advice-on-g4autolock-in-primary-generator/15421/3 "2026-11-04T08:18:18Z")

</div>

Thank you! In fact, using `G4AutoLock lock(&myPrimGenMutex);` within the `GeneratePrimaries(G4Event* anEvent)` function significantly slows down the computation speed, so I wanted to clarify the correct way to use it.
