ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
threadsh1.C
Go to the documentation of this file.
1 //+ Example of a simple script creating 3 threads.
2 // This script can only be executed via ACliC .x threadsh1.C++.
3 
4 #include "TCanvas.h"
5 #include "TFrame.h"
6 #include "TH1F.h"
7 #include "TRandom.h"
8 #include "TThread.h"
9 
10 
11 TCanvas *c[4];
12 TH1F *hpx[4];
13 TThread *t[5];
15 
16 void *handle(void *ptr)
17 {
18  long nr = (long) ptr;
19  Long64_t nfills = 25000000;
20  int upd = 50000;
21 
22  char name[32];
23  sprintf(name,"hpx%ld",nr);
24  TThread::Lock();
25  hpx[nr] = new TH1F(name,"This is the px distribution",100,-4,4);
26  hpx[nr]->SetFillColor(48);
28  Float_t px, py, pz;
29  gRandom->SetSeed();
30  for (Int_t i = 0; i < nfills; i++) {
31  gRandom->Rannor(px,py);
32  pz = px*px + py*py;
33  hpx[nr]->Fill(px);
34  if (i && (i%upd) == 0) {
35  if (i == upd) {
36  TThread::Lock();
37  c[nr]->cd();
38  hpx[nr]->Draw();
40  }
41  if (c[nr]) c[nr]->Modified();
42  gSystem->Sleep(10);
43  }
44  }
45  return 0;
46 }
47 
48 void *joiner(void *)
49 {
50  t[0]->Join();
51  t[1]->Join();
52  t[2]->Join();
53  t[3]->Join();
54 
55  finished = kTRUE;
56 
57  return 0;
58 }
59 
60 void closed(Int_t id)
61 {
62  // kill the thread matching the canvas being closed
63  t[id]->Kill();
64  // and set the canvas pointer to 0
65  c[id] = 0;
66 }
67 
68 void threadsh1()
69 {
70 #ifdef __CINT__
71  printf("This script can only be executed via ACliC: .x threadsh1.C++\n");
72  return;
73 #endif
74 
75  finished = kFALSE;
76  //gDebug = 1;
77 
78  c[0] = new TCanvas("c0","Dynamic Filling Example",100,20,400,300);
79  c[0]->SetFillColor(42);
80  c[0]->GetFrame()->SetFillColor(21);
81  c[0]->GetFrame()->SetBorderSize(6);
82  c[0]->GetFrame()->SetBorderMode(-1);
83  c[1] = new TCanvas("c1","Dynamic Filling Example",510,20,400,300);
84  c[1]->SetFillColor(42);
85  c[1]->GetFrame()->SetFillColor(21);
86  c[1]->GetFrame()->SetBorderSize(6);
87  c[1]->GetFrame()->SetBorderMode(-1);
88  c[2] = new TCanvas("c2","Dynamic Filling Example",100,350,400,300);
89  c[2]->SetFillColor(42);
90  c[2]->GetFrame()->SetFillColor(21);
91  c[2]->GetFrame()->SetBorderSize(6);
92  c[2]->GetFrame()->SetBorderMode(-1);
93  c[3] = new TCanvas("c3","Dynamic Filling Example",510,350,400,300);
94  c[3]->SetFillColor(42);
95  c[3]->GetFrame()->SetFillColor(21);
96  c[3]->GetFrame()->SetBorderSize(6);
97  c[3]->GetFrame()->SetBorderMode(-1);
98 
99  // connect to the Closed() signal to kill the thread when a canvas is closed
100  c[0]->Connect("Closed()", 0, 0, "closed(Int_t=0)");
101  c[1]->Connect("Closed()", 0, 0, "closed(Int_t=1)");
102  c[2]->Connect("Closed()", 0, 0, "closed(Int_t=2)");
103  c[3]->Connect("Closed()", 0, 0, "closed(Int_t=3)");
104 
105  printf("Starting Thread 0\n");
106  t[0] = new TThread("t0", handle, (void*) 0);
107  t[0]->Run();
108  printf("Starting Thread 1\n");
109  t[1] = new TThread("t1", handle, (void*) 1);
110  t[1]->Run();
111  printf("Starting Thread 2\n");
112  t[2] = new TThread("t2", handle, (void*) 2);
113  t[2]->Run();
114  printf("Starting Thread 3\n");
115  t[3] = new TThread("t3", handle, (void*) 3);
116  t[3]->Run();
117  printf("Starting Thread 4\n");
118  t[4] = new TThread("t4", joiner, (void*) 3);
119  t[4]->Run();
120 
121  TThread::Ps();
122 
123  while (!finished) {
124  for (int i = 0; i < 4; i++) {
125  if (c[i] && c[i]->IsModified()) {
126  //printf("Update canvas %d\n", i);
127  c[i]->Update();
128  }
129  }
130  gSystem->Sleep(100);
132  }
133 
134  t[4]->Join();
135  TThread::Ps();
136 
137  delete t[0];
138  delete t[1];
139  delete t[2];
140  delete t[3];
141  delete t[4];
142 }
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3159
virtual Bool_t ProcessEvents()
Process pending events (GUI, timers, sockets).
Definition: TSystem.cxx:420
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
Float_t pz
Definition: hprod.C:33
long long Long64_t
Definition: RtypesCore.h:69
void * handle(void *ptr)
Definition: threadsh1.C:16
float Float_t
Definition: RtypesCore.h:53
virtual void SetBorderMode(Short_t bordermode)
Definition: TWbox.h:62
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:659
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:570
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
void closed(Int_t id)
Definition: threadsh1.C:60
Float_t py
Definition: hprod.C:33
virtual void SetSeed(UInt_t seed=0)
Set the random generator seed.
Definition: TRandom.cxx:568
void * joiner(void *)
Definition: threadsh1.C:48
virtual void Sleep(UInt_t milliSec)
Sleep milliSec milli seconds.
Definition: TSystem.cxx:441
TFrame * GetFrame()
Get frame.
Definition: TPad.cxx:2729
Bool_t finished
Definition: threadsh1.C:14
XFontStruct * id
Definition: TGX11.cxx:108
Int_t Run(void *arg=0)
Start the thread.
Definition: TThread.cxx:552
virtual void SetBorderSize(Short_t bordersize)
Definition: TWbox.h:63
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:1135
R__EXTERN TSystem * gSystem
Definition: TSystem.h:545
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2878
virtual void SetFillColor(Color_t fcolor)
Definition: TAttFill.h:50
void threadsh1()
Definition: threadsh1.C:68
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
The Canvas class.
Definition: TCanvas.h:48
static Int_t UnLock()
Static method to unlock the main thread mutex.
Definition: TThread.cxx:774
ClassImp(TMCParticle) void TMCParticle printf(": p=(%7.3f,%7.3f,%9.3f) ;", fPx, fPy, fPz)
#define name(a, b)
Definition: linkTestLib0.cpp:5
Float_t px
Definition: hprod.C:33
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2179
const Bool_t kTRUE
Definition: Rtypes.h:91
void Modified(Bool_t flag=1)
Definition: TPad.h:407