Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
17TCanvas *c[4];
18TH1F *hpx[4];
19TThread *t[5];
20TRandom * rng[4];
21Bool_t finished;
22
23void *handle(void *ptr)
24{
25 long nr = (long) ptr;
26 Long64_t nfills = 25000000;
27 int upd = 50000;
28
29 char name[32];
30 sprintf(name,"hpx%ld",nr);
32 hpx[nr] = new TH1F(name,"This is the px distribution",100,-4,4);
33 hpx[nr]->SetFillColor(48);
35 Float_t px, py, pz;
36 rng[nr]->SetSeed(0);
37 for (Int_t i = 0; i < nfills; i++) {
38 rng[nr]->Rannor(px,py);
39 pz = px*px + py*py;
40 hpx[nr]->Fill(px);
41 if (i && (i%upd) == 0) {
42 if (i == upd) {
44 c[nr]->cd();
45 hpx[nr]->Draw();
47 }
48 if (c[nr]) c[nr]->Modified();
49 gSystem->Sleep(10);
50 }
51 }
52 return 0;
53}
54
55void *joiner(void *)
56{
57 t[0]->Join();
58 t[1]->Join();
59 t[2]->Join();
60 t[3]->Join();
61
62 finished = kTRUE;
63
64 return 0;
65}
66
67void closed(Int_t id)
68{
69 // kill the thread matching the canvas being closed
70 t[id]->Kill();
71 // and set the canvas pointer to 0
72 c[id] = 0;
73}
74
75void threadsh1()
76{
77
78 finished = kFALSE;
79 //gDebug = 1;
80
81 c[0] = new TCanvas("c0","Dynamic Filling Example",100,20,400,300);
82 c[0]->SetFillColor(42);
83 c[0]->GetFrame()->SetFillColor(21);
84 c[0]->GetFrame()->SetBorderSize(6);
85 c[0]->GetFrame()->SetBorderMode(-1);
86 c[1] = new TCanvas("c1","Dynamic Filling Example",510,20,400,300);
87 c[1]->SetFillColor(42);
88 c[1]->GetFrame()->SetFillColor(21);
89 c[1]->GetFrame()->SetBorderSize(6);
90 c[1]->GetFrame()->SetBorderMode(-1);
91 c[2] = new TCanvas("c2","Dynamic Filling Example",100,350,400,300);
92 c[2]->SetFillColor(42);
93 c[2]->GetFrame()->SetFillColor(21);
94 c[2]->GetFrame()->SetBorderSize(6);
95 c[2]->GetFrame()->SetBorderMode(-1);
96 c[3] = new TCanvas("c3","Dynamic Filling Example",510,350,400,300);
97 c[3]->SetFillColor(42);
98 c[3]->GetFrame()->SetFillColor(21);
99 c[3]->GetFrame()->SetBorderSize(6);
100 c[3]->GetFrame()->SetBorderMode(-1);
101
102 // connect to the Closed() signal to kill the thread when a canvas is closed
103 c[0]->Connect("Closed()", 0, 0, "closed(Int_t=0)");
104 c[1]->Connect("Closed()", 0, 0, "closed(Int_t=1)");
105 c[2]->Connect("Closed()", 0, 0, "closed(Int_t=2)");
106 c[3]->Connect("Closed()", 0, 0, "closed(Int_t=3)");
107
108 rng[0] = new TRandom3(1);
109 rng[1] = new TRandom3(2);
110 rng[2] = new TRandom3(3);
111 rng[3] = new TRandom3(4);
112
113 printf("Starting Thread 0\n");
114 t[0] = new TThread("t0", handle, (void*) 0);
115 t[0]->Run();
116 printf("Starting Thread 1\n");
117 t[1] = new TThread("t1", handle, (void*) 1);
118 t[1]->Run();
119 printf("Starting Thread 2\n");
120 t[2] = new TThread("t2", handle, (void*) 2);
121 t[2]->Run();
122 printf("Starting Thread 3\n");
123 t[3] = new TThread("t3", handle, (void*) 3);
124 t[3]->Run();
125 printf("Starting Thread 4\n");
126 t[4] = new TThread("t4", joiner, (void*) 3);
127 t[4]->Run();
128
129 TThread::Ps();
130
131 while (!finished) {
132 for (int i = 0; i < 4; i++) {
133 if (c[i] && c[i]->IsModified()) {
134 //printf("Update canvas %d\n", i);
135 c[i]->Update();
136 }
137 }
138 gSystem->Sleep(100);
140 }
141
142 t[4]->Join();
143 TThread::Ps();
144
145 delete t[0];
146 delete t[1];
147 delete t[2];
148 delete t[3];
149 delete t[4];
150
151 delete rng[0];
152 delete rng[1];
153 delete rng[2];
154 delete rng[3];
155}
long
#define c(i)
Definition RSha256.hxx:101
int Int_t
Definition RtypesCore.h:45
const Bool_t kFALSE
Definition RtypesCore.h:92
bool Bool_t
Definition RtypesCore.h:63
long long Long64_t
Definition RtypesCore.h:73
float Float_t
Definition RtypesCore.h:57
const Bool_t kTRUE
Definition RtypesCore.h:91
XFontStruct * id
Definition TGX11.cxx:109
char name[80]
Definition TGX11.cxx:110
R__EXTERN TSystem * gSystem
Definition TSystem.h:559
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition TAttFill.h:37
The Canvas class.
Definition TCanvas.h:23
1-D histogram with a float per channel (see TH1 documentation)}
Definition TH1.h:575
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition TH1.cxx:3350
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition TH1.cxx:3073
Random number generator class based on M.
Definition TRandom3.h:27
This is the base class for the ROOT Random number generators.
Definition TRandom.h:27
virtual void SetSeed(ULong_t seed=0)
Set the random generator seed.
Definition TRandom.cxx:608
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:500
virtual void Sleep(UInt_t milliSec)
Sleep milliSec milli seconds.
Definition TSystem.cxx:438
virtual Bool_t ProcessEvents()
Process pending events (GUI, timers, sockets).
Definition TSystem.cxx:417
static void Ps()
Static method listing the existing threads.
Definition TThread.cxx:845
static Int_t UnLock()
Static method to unlock the main thread mutex.
Definition TThread.cxx:790
Int_t Kill()
Kill this thread.
Definition TThread.cxx:592
Long_t Join(void **ret=nullptr)
Join this thread.
Definition TThread.cxx:515
static Int_t Lock()
Static method to lock the main thread mutex.
Definition TThread.cxx:774
Int_t Run(void *arg=nullptr)
Start the thread.
Definition TThread.cxx:568