Handling Ctrl+C/Sigint in multithreaded run

Hello everyone,

I want to handle Ctrl+C event (crossplatform on Windows/Linux) to close my application, storing all status of detectors and histograms.
When G4 was singlethreaded it was simple, I had

G4RunManager *runManagerTop;

before main, code like

#if defined (WIN32)
BOOL CtrlHandler( DWORD fdwCtrlType )
{
G4String a;
G4double pt;
G4int pn;
switch( fdwCtrlType )
{
case CTRL_C_EVENT:
printf( “Ctrl-C event got\n\n” );
((RunAction*)runManagerTop->GetUserRunAction())->eofRunAction(1);
runManagerTop->AbortRun();
return FALSE;

case CTRL_BREAK_EVENT: 
  printf( "Ctrl-Break event\n\n" );
  return( TRUE );

default: 
  return FALSE; 

}
}
#else
void CtrlBreakBackend(int sig) {
((RunAction*)runManagerTop->GetUserRunAction())->eofRunAction(1);
runManagerTop->AbortRun();
}
#endif
void handleCtrlCEventInit(G4RunManager *runManagerMain) {
runManagerTop = runManagerMain;
#if defined (WIN32)
SetConsoleCtrlHandler( (PHANDLER_ROUTINE) CtrlHandler, TRUE );
#else
#ifdef SIGBREAK
signal(SIGBREAK, CtrlBreakBackend);
#endif
#endif

and called from main after instantiating

G4RunManager * runManager = new G4RunManager;
handleCtrlCEventInit(runManager);

(eofRunAction(1) is my user function at RunAction that handles the saving).

But with multitreaded G4 it fails to work,
First, runManagerTop->AbortRun() seems to do nothing,
Secondly, I cannot access merge function of Run to put all data together.

Are there a proper way to handle Ctrl+C/Sigint signal in multithreaded application?

1 Like

Hello,

you may consult Geant4 examples, many are working in MT mode: $G4INSTALL/examples/extended/electromagnetic/TestEm3, $G4INSTALL/examples/basic, others.

VI

Hello,

these examples not handling interrupting the run via extend command (Ctrl+C/SIGINT in my case).
For TestEm3 e.g. if I click Ctrl+C while running on Windows, I get in stdout

WARNING - Attempt to delete the solid store while geometry closed !
WARNING - Attempt to delete the logical volume store while geometry closed !
WARNING - Attempt to delete the physical volume store while geometry closed !
WARNING - Attempt to delete the region store while geometry closed !
^C

and the empty root-file.

I want that my Ctrl+C event correctly do all the EndOfRunAction features, merge and store root files and my detectors.