Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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);
33  TThread::UnLock();
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();
45  TThread::UnLock();
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);
133  gSystem->ProcessEvents();
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 }