<commit_before>#include "TApplication.h"
#include "TObject.h"
#include "TH1.h"
#include "TMath.h"
#include "TCanvas.h"
#include "TRandom.h"
#include "TThread.h"

/*
** Module name : threads
**
** Description :
**  This file implements four functions, which shall perform some
**  actions on objects like histograms and a canvas.
*/


// a global canvas for the drawing of histograms
TCanvas *c1;
// a pad for the histogram, will be used by thread mhs
TPad *pad1;
// a pad for the histogram, will be used by thread mhs2
TPad *pad2;

// a global histogram object, which will be accessed both by mhs and mhs1
TH1F *total0;

// thread function (filling histogram main and drawing total0 on pad1)
void *mhs(void *)
{
   TThread::Printf("Start of mhs");

   // change to the canvas and draw its correspondig pad 1
   c1->cd();
   c1->SetGrid();
   pad1->Draw();

   // creating a histogram is explicitely locked by a global mutex
   TThread::Lock();
   TH1F *main   = new TH1F("main","Main contributor",100,-4,4);
   TThread::UnLock();

   // other operations needn't be locked
   total0->Sumw2();
   total0->SetMarkerStyle(21);
   total0->SetMarkerSize(0.7);
   main->SetFillColor(16);

   // Fill histogram main randomly
   gRandom->SetSeed();
   const Int_t kUPDATE = 100;
   Float_t xmain;
   // run for a certain amount of events or
   //for ( Int_t i=0; i<=100000; i++) {
   // run forever
   for ( Int_t i=0; ; i++) {
      xmain = gRandom->Gaus(-1,1.5);
      // filling of a histogram is locked to avoid data loss
      TThread::Lock();
      main->Fill(xmain);
      total0->Fill(xmain);
      TThread::UnLock();
      // drawing of the histogram is locked automatically by ROOT
      if (!(i%kUPDATE)) {
         if (i == kUPDATE) {
            pad1->cd();
            total0->Draw("e1p");
         }
         pad1->cd();
         total0->Draw("e1p");
         c1->Modified();
         c1->Update();
      }
      // sleep for 1 ms: sleep not necessary, slows things only a bit down
      // because the threads are actually doing nothing, which is eventually
      // very fast ;-)
      gSystem->Sleep(1);
   }
   TThread::Printf("End of mhs\n");

   c1->Modified();
   c1->Update();
   return 0;
}

// thread function (filling total0)
void *mhs1(void *)
{
   TThread::Printf("Start of mhs1");

   // instantiation of objects has to be locked explicitly
   TThread::Lock();
   TH1F *s1     = new TH1F("s1","This is the first signal",100,-4,4);
   TH1F *s2     = new TH1F("s2","This is the second signal",100,-4,4);
   TThread::UnLock();

   s1->SetFillColor(42);
   s2->SetFillColor(46);

   // Fill histograms randomly
   gRandom->SetSeed();
   Float_t xs1, xs2;
   //for ( Int_t i=0; i<=100000; i++) {
   for ( Int_t i=0; ; i++) {
      xs1   = gRandom->Gaus(-0.5,0.5);
      xs2   = gRandom->Gaus(1,0.3);
      // locking avoids data loss
      TThread::Lock();
      s1->Fill(xs1,0.3);
      s2->Fill(xs2,0.2);
      total0->Fill(xs1,0.3);
      total0->Fill(xs2,0.2);
      TThread::UnLock();
      // sleep for 6 ms: not necessary (see above)
      gSystem->Sleep(6);
   }
   TThread::Printf("End of mhs1\n");

   // wo is last draws the histogram a last time
   pad1->cd();
   total0->Draw("e1p");
   c1->Modified();
   c1->Update();
   return 0;
}

// thread function: plays with its own histograms, draw on canvas in pad2
void *mhs2(void *)
{
   TThread::Printf("Start of mhs2");

   c1->cd();
   pad2->Draw();

   // Create some histograms: explicitly locked.
   TThread::Lock();
   TH1F *total  = new TH1F("total2","This is the total distribution",100,-4,4);
   TH1F *main   = new TH1F("main2","Main contributor",100,-4,4);
   TH1F *s1     = new TH1F("s12","This is the first signal",100,-4,4);
   TH1F *s2     = new TH1F("s22","This is the second signal",100,-4,4);
   TThread::UnLock();

   total->Sumw2();
   total->SetMarkerStyle(21);
   total->SetMarkerSize(0.7);
   main->SetFillColor(16);
   s1->SetFillColor(42);
   s2->SetFillColor(46);

   // Fill histograms randomly
   gRandom->SetSeed();
   const Int_t kUPDATE = 100;
   Float_t xs1, xs2, xmain;
   // for ( Int_t i=0; i<=100000; i++) {
   for ( Int_t i=0; ; i++) {
      xmain = gRandom->Gaus(-1,1.5);
      xs1   = gRandom->Gaus(-0.5,0.5);
      xs2   = gRandom->Gaus(1,0.3);
      // no locking here because nobody else uses these objects
      main->Fill(xmain);
      s1->Fill(xs1,0.3);
      s2->Fill(xs2,0.2);
      total->Fill(xmain);
      total->Fill(xs1,0.3);
      total->Fill(xs2,0.2);
      if (!(i%kUPDATE)) {
         if (i == kUPDATE) {
            pad2->cd();
            total->Draw("e1p");
         }
         pad2->cd();
         total->Draw("e1p");
         c1->Modified();
         c1->Update();
      }
      // sleep for 1 ms: not necessary
      gSystem->Sleep(1);
   }
   TThread::Printf("End of mhs2\n");
   c1->Modified();
   c1->Update();
   return 0;
}

// thread to run Ps(): perform every 5 seconds a TThread::Ps()
void *top(void *)
{
   TThread::Printf("Start of top");

   for (Int_t i = 0; ;i++) {
      TThread::Ps();
      gSystem->Sleep(5000);
   }
   TThread::Printf("End of top");
   return 0;
}<commit_msg>From Bertrand: make program work on Windows (TH1::Draw() is not thread safe).<commit_after>#include "TApplication.h"
#include "TObject.h"
#include "TH1.h"
#include "TMath.h"
#include "TCanvas.h"
#include "TRandom.h"
#include "TThread.h"

/*
** Module name : threads
**
** Description :
**  This file implements four functions, which shall perform some
**  actions on objects like histograms and a canvas.
*/


// a global canvas for the drawing of histograms
TCanvas *c1;
// a pad for the histogram, will be used by thread mhs
TPad *pad1;
// a pad for the histogram, will be used by thread mhs2
TPad *pad2;

// a global histogram object, which will be accessed both by mhs and mhs1
TH1F *total0;

// thread function (filling histogram main and drawing total0 on pad1)
void *mhs(void *)
{
   TThread::Printf("Start of mhs");

   // change to the canvas and draw its correspondig pad 1
   c1->cd();
   c1->SetGrid();
   pad1->Draw();

   // creating a histogram is explicitely locked by a global mutex
   TThread::Lock();
   TH1F *main   = new TH1F("main","Main contributor",100,-4,4);
   TThread::UnLock();

   // other operations needn't be locked
   total0->Sumw2();
   total0->SetMarkerStyle(21);
   total0->SetMarkerSize(0.7);
   main->SetFillColor(16);

   // Fill histogram main randomly
   gRandom->SetSeed();
   const Int_t kUPDATE = 100;
   Float_t xmain;
   // run for a certain amount of events or
   //for ( Int_t i=0; i<=100000; i++) {
   // run forever
   for ( Int_t i=0; ; i++) {
      xmain = gRandom->Gaus(-1,1.5);
      // filling of a histogram is locked to avoid data loss
      TThread::Lock();
      main->Fill(xmain);
      total0->Fill(xmain);
      TThread::UnLock();
      // drawing of the histogram is locked automatically by ROOT
      if (!(i%kUPDATE)) {
         if (i == kUPDATE) {
            pad1->cd();
            total0->Draw("e1p");
         }
         pad1->Modified();
         pad1->Update();
         c1->Modified();
         c1->Update();
      }
      // sleep for 1 ms: sleep not necessary, slows things only a bit down
      // because the threads are actually doing nothing, which is eventually
      // very fast ;-)
      gSystem->Sleep(1);
   }
   TThread::Printf("End of mhs\n");

   c1->Modified();
   c1->Update();
   return 0;
}

// thread function (filling total0)
void *mhs1(void *)
{
   TThread::Printf("Start of mhs1");

   // instantiation of objects has to be locked explicitly
   TThread::Lock();
   TH1F *s1     = new TH1F("s1","This is the first signal",100,-4,4);
   TH1F *s2     = new TH1F("s2","This is the second signal",100,-4,4);
   TThread::UnLock();

   s1->SetFillColor(42);
   s2->SetFillColor(46);

   // Fill histograms randomly
   gRandom->SetSeed();
   Float_t xs1, xs2;
   //for ( Int_t i=0; i<=100000; i++) {
   for ( Int_t i=0; ; i++) {
      xs1   = gRandom->Gaus(-0.5,0.5);
      xs2   = gRandom->Gaus(1,0.3);
      // locking avoids data loss
      TThread::Lock();
      s1->Fill(xs1,0.3);
      s2->Fill(xs2,0.2);
      total0->Fill(xs1,0.3);
      total0->Fill(xs2,0.2);
      TThread::UnLock();
      // sleep for 6 ms: not necessary (see above)
      gSystem->Sleep(6);
   }
   TThread::Printf("End of mhs1\n");

   // wo is last draws the histogram a last time
   pad1->cd();
   total0->Draw("e1p");
   c1->Modified();
   c1->Update();
   return 0;
}

// thread function: plays with its own histograms, draw on canvas in pad2
void *mhs2(void *)
{
   TThread::Printf("Start of mhs2");

   c1->cd();
   pad2->Draw();

   // Create some histograms: explicitly locked.
   TThread::Lock();
   TH1F *total  = new TH1F("total2","This is the total distribution",100,-4,4);
   TH1F *main   = new TH1F("main2","Main contributor",100,-4,4);
   TH1F *s1     = new TH1F("s12","This is the first signal",100,-4,4);
   TH1F *s2     = new TH1F("s22","This is the second signal",100,-4,4);
   TThread::UnLock();

   total->Sumw2();
   total->SetMarkerStyle(21);
   total->SetMarkerSize(0.7);
   main->SetFillColor(16);
   s1->SetFillColor(42);
   s2->SetFillColor(46);

   // Fill histograms randomly
   gRandom->SetSeed();
   const Int_t kUPDATE = 100;
   Float_t xs1, xs2, xmain;
   // for ( Int_t i=0; i<=100000; i++) {
   for ( Int_t i=0; ; i++) {
      xmain = gRandom->Gaus(-1,1.5);
      xs1   = gRandom->Gaus(-0.5,0.5);
      xs2   = gRandom->Gaus(1,0.3);
      // no locking here because nobody else uses these objects
      main->Fill(xmain);
      s1->Fill(xs1,0.3);
      s2->Fill(xs2,0.2);
      total->Fill(xmain);
      total->Fill(xs1,0.3);
      total->Fill(xs2,0.2);
      if (!(i%kUPDATE)) {
         if (i == kUPDATE) {
            pad2->cd();
            total->Draw("e1p");
         }
         pad2->Modified();
         pad2->Update();
         c1->Modified();
         c1->Update();
      }
      // sleep for 1 ms: not necessary
      gSystem->Sleep(1);
   }
   TThread::Printf("End of mhs2\n");
   c1->Modified();
   c1->Update();
   return 0;
}

// thread to run Ps(): perform every 5 seconds a TThread::Ps()
void *top(void *)
{
   TThread::Printf("Start of top");

   for (Int_t i = 0; ;i++) {
      TThread::Ps();
      gSystem->Sleep(5000);
   }
   TThread::Printf("End of top");
   return 0;
}
{'fraction_non_alphanumeric': 0.16581113801452785, 'fraction_numerical': 0.04687651331719128, 'mean_word_length': 2.566839378238342, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 127, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6135052', 'n_tokens_mistral': 3870, 'n_tokens_neox': 3655, 'n_words': 987}