Logo ROOT   6.10/09
Reference Guide
threadsh1.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_thread
3 /// Example of a simple script creating 3 threads.
4 /// This script can only be executed via ACliC .x threadsh1.C++.
5 ///
6 /// \macro_code
7 ///
8 /// \author Victor Perevovchikov
9 
10 #include "TCanvas.h"
11 #include "TFrame.h"
12 #include "TH1F.h"
13 #include "TRandom.h"
14 #include "TThread.h"
15 
16 
17 TCanvas *c[4];
18 TH1F *hpx[4];
19 TThread *t[5];
20 Bool_t finished;
21 
22 void *handle(void *ptr)
23 {
24  long nr = (long) ptr;
25  Long64_t nfills = 25000000;
26  int upd = 50000;
27 
28  char name[32];
29  sprintf(name,"hpx%ld",nr);
30  TThread::Lock();
31  hpx[nr] = new TH1F(name,"This is the px distribution",100,-4,4);
32  hpx[nr]->SetFillColor(48);
34  Float_t px, py, pz;
35  gRandom->SetSeed();
36  for (Int_t i = 0; i < nfills; i++) {
37  gRandom->Rannor(px,py);
38  pz = px*px + py*py;
39  hpx[nr]->Fill(px);
40  if (i && (i%upd) == 0) {
41  if (i == upd) {
42  TThread::Lock();
43  c[nr]->cd();
44  hpx[nr]->Draw();
46  }
47  if (c[nr]) c[nr]->Modified();
48  gSystem->Sleep(10);
49  }
50  }
51  return 0;
52 }
53 
54 void *joiner(void *)
55 {
56  t[0]->Join();
57  t[1]->Join();
58  t[2]->Join();
59  t[3]->Join();
60 
61  finished = kTRUE;
62 
63  return 0;
64 }
65 
66 void closed(Int_t id)
67 {
68  // kill the thread matching the canvas being closed
69  t[id]->Kill();
70  // and set the canvas pointer to 0
71  c[id] = 0;
72 }
73 
74 void threadsh1()
75 {
76 
77  finished = kFALSE;
78  //gDebug = 1;
79 
80  c[0] = new TCanvas("c0","Dynamic Filling Example",100,20,400,300);
81  c[0]->SetFillColor(42);
82  c[0]->GetFrame()->SetFillColor(21);
83  c[0]->GetFrame()->SetBorderSize(6);
84  c[0]->GetFrame()->SetBorderMode(-1);
85  c[1] = new TCanvas("c1","Dynamic Filling Example",510,20,400,300);
86  c[1]->SetFillColor(42);
87  c[1]->GetFrame()->SetFillColor(21);
88  c[1]->GetFrame()->SetBorderSize(6);
89  c[1]->GetFrame()->SetBorderMode(-1);
90  c[2] = new TCanvas("c2","Dynamic Filling Example",100,350,400,300);
91  c[2]->SetFillColor(42);
92  c[2]->GetFrame()->SetFillColor(21);
93  c[2]->GetFrame()->SetBorderSize(6);
94  c[2]->GetFrame()->SetBorderMode(-1);
95  c[3] = new TCanvas("c3","Dynamic Filling Example",510,350,400,300);
96  c[3]->SetFillColor(42);
97  c[3]->GetFrame()->SetFillColor(21);
98  c[3]->GetFrame()->SetBorderSize(6);
99  c[3]->GetFrame()->SetBorderMode(-1);
100 
101  // connect to the Closed() signal to kill the thread when a canvas is closed
102  c[0]->Connect("Closed()", 0, 0, "closed(Int_t=0)");
103  c[1]->Connect("Closed()", 0, 0, "closed(Int_t=1)");
104  c[2]->Connect("Closed()", 0, 0, "closed(Int_t=2)");
105  c[3]->Connect("Closed()", 0, 0, "closed(Int_t=3)");
106 
107  printf("Starting Thread 0\n");
108  t[0] = new TThread("t0", handle, (void*) 0);
109  t[0]->Run();
110  printf("Starting Thread 1\n");
111  t[1] = new TThread("t1", handle, (void*) 1);
112  t[1]->Run();
113  printf("Starting Thread 2\n");
114  t[2] = new TThread("t2", handle, (void*) 2);
115  t[2]->Run();
116  printf("Starting Thread 3\n");
117  t[3] = new TThread("t3", handle, (void*) 3);
118  t[3]->Run();
119  printf("Starting Thread 4\n");
120  t[4] = new TThread("t4", joiner, (void*) 3);
121  t[4]->Run();
122 
123  TThread::Ps();
124 
125  while (!finished) {
126  for (int i = 0; i < 4; i++) {
127  if (c[i] && c[i]->IsModified()) {
128  //printf("Update canvas %d\n", i);
129  c[i]->Update();
130  }
131  }
132  gSystem->Sleep(100);
134  }
135 
136  t[4]->Join();
137  TThread::Ps();
138 
139  delete t[0];
140  delete t[1];
141  delete t[2];
142  delete t[3];
143  delete t[4];
144 }
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3126
virtual Bool_t ProcessEvents()
Process pending events (GUI, timers, sockets).
Definition: TSystem.cxx:423
virtual void Rannor(Float_t &a, Float_t &b)
Return 2 numbers distributed following a gaussian with mean=0 and sigma=1.
Definition: TRandom.cxx:460
long long Long64_t
Definition: RtypesCore.h:69
float Float_t
Definition: RtypesCore.h:53
virtual void SetBorderMode(Short_t bordermode)
Definition: TWbox.h:49
THist< 1, float, THistStatContent, THistStatUncertainty > TH1F
Definition: THist.hxx:311
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:679
tomato 1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:551
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
virtual void Sleep(UInt_t milliSec)
Sleep milliSec milli seconds.
Definition: TSystem.cxx:444
TFrame * GetFrame()
Get frame.
Definition: TPad.cxx:2794
virtual void SetSeed(ULong_t seed=0)
Set the random generator seed.
Definition: TRandom.cxx:568
Int_t Run(void *arg=0)
Start the thread.
Definition: TThread.cxx:552
virtual void SetBorderSize(Short_t bordersize)
Definition: TWbox.h:50
Bool_t Connect(const char *signal, const char *receiver_class, void *receiver, const char *slot)
Non-static method is used to connect from the signal of this object to the receiver slot...
Definition: TQObject.cxx:867
R__EXTERN TSystem * gSystem
Definition: TSystem.h:539
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2851
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition: TAttFill.h:37
Long_t Join(void **ret=0)
Join this thread.
Definition: TThread.cxx:499
Int_t Kill()
Kill this thread.
Definition: TThread.cxx:576
static Int_t Lock()
Static method to lock the main thread mutex.
Definition: TThread.cxx:758
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
static void Ps()
Static method listing the existing threads.
Definition: TThread.cxx:829
const Bool_t kFALSE
Definition: RtypesCore.h:92
The Canvas class.
Definition: TCanvas.h:31
static Int_t UnLock()
Static method to unlock the main thread mutex.
Definition: TThread.cxx:774
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2208
const Bool_t kTRUE
Definition: RtypesCore.h:91
void Modified(Bool_t flag=1)
Definition: TPad.h:410