How to write results files for each thread

Dear all,
I want to write result files for each threads using Sensitive Detector

  for (int i = G4Threading::GetNumberOfRunningWorkerThreads(); i >= 1; i--)
  {
    if(event_id == (i*(Total_Events_To_Be_Processed/3)-1)) 
    {
      G4cout << "Reach in an event to write data " << G4endl;
      G4cout << "Total events: " << Total_Events_To_Be_Processed << G4endl;
      G4cout << "Event ID: " << event_id << G4endl;
      G4cout << "Thread ID: " << i << G4endl;

      SaveDataInBinaryFile();
    }
  }
  • If I use 3 threads to simulate 100 histories, everything will be done.
  • If I simulate 1000 histories, there is an error that caused my code to be incorrect. Thread number 1 is not activated.

    How can I solve it? Thank you in advance.