Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
threadsh1.C File Reference

Detailed Description

Example of a simple script creating 3 threads.

This script can only be executed via ACliC .x threadsh1.C++.

#include "TCanvas.h"
#include "TFrame.h"
#include "TH1F.h"
#include "TRandom.h"
#include "TThread.h"
TCanvas *c[4];
TH1F *hpx[4];
TThread *t[5];
TRandom * rng[4];
Bool_t finished;
void *handle(void *ptr)
{
long nr = (long) ptr;
Long64_t nfills = 25000000;
int upd = 50000;
char name[32];
sprintf(name,"hpx%ld",nr);
hpx[nr] = new TH1F(name,"This is the px distribution",100,-4,4);
hpx[nr]->SetFillColor(48);
Float_t px, py, pz;
rng[nr]->SetSeed(0);
for (Int_t i = 0; i < nfills; i++) {
rng[nr]->Rannor(px,py);
pz = px*px + py*py;
hpx[nr]->Fill(px);
if (i && (i%upd) == 0) {
if (i == upd) {
c[nr]->cd();
hpx[nr]->Draw();
}
if (c[nr]) c[nr]->Modified();
gSystem->Sleep(10);
}
}
return 0;
}
void *joiner(void *)
{
t[0]->Join();
t[1]->Join();
t[2]->Join();
t[3]->Join();
finished = kTRUE;
return 0;
}
void closed(Int_t id)
{
// kill the thread matching the canvas being closed
t[id]->Kill();
// and set the canvas pointer to 0
c[id] = 0;
}
void threadsh1()
{
finished = kFALSE;
//gDebug = 1;
c[0] = new TCanvas("c0","Dynamic Filling Example",100,20,400,300);
c[0]->SetFillColor(42);
c[0]->GetFrame()->SetFillColor(21);
c[0]->GetFrame()->SetBorderSize(6);
c[0]->GetFrame()->SetBorderMode(-1);
c[1] = new TCanvas("c1","Dynamic Filling Example",510,20,400,300);
c[1]->SetFillColor(42);
c[1]->GetFrame()->SetFillColor(21);
c[1]->GetFrame()->SetBorderSize(6);
c[1]->GetFrame()->SetBorderMode(-1);
c[2] = new TCanvas("c2","Dynamic Filling Example",100,350,400,300);
c[2]->SetFillColor(42);
c[2]->GetFrame()->SetFillColor(21);
c[2]->GetFrame()->SetBorderSize(6);
c[2]->GetFrame()->SetBorderMode(-1);
c[3] = new TCanvas("c3","Dynamic Filling Example",510,350,400,300);
c[3]->SetFillColor(42);
c[3]->GetFrame()->SetFillColor(21);
c[3]->GetFrame()->SetBorderSize(6);
c[3]->GetFrame()->SetBorderMode(-1);
// connect to the Closed() signal to kill the thread when a canvas is closed
c[0]->Connect("Closed()", 0, 0, "closed(Int_t=0)");
c[1]->Connect("Closed()", 0, 0, "closed(Int_t=1)");
c[2]->Connect("Closed()", 0, 0, "closed(Int_t=2)");
c[3]->Connect("Closed()", 0, 0, "closed(Int_t=3)");
rng[0] = new TRandom3(1);
rng[1] = new TRandom3(2);
rng[2] = new TRandom3(3);
rng[3] = new TRandom3(4);
printf("Starting Thread 0\n");
t[0] = new TThread("t0", handle, (void*) 0);
t[0]->Run();
printf("Starting Thread 1\n");
t[1] = new TThread("t1", handle, (void*) 1);
t[1]->Run();
printf("Starting Thread 2\n");
t[2] = new TThread("t2", handle, (void*) 2);
t[2]->Run();
printf("Starting Thread 3\n");
t[3] = new TThread("t3", handle, (void*) 3);
t[3]->Run();
printf("Starting Thread 4\n");
t[4] = new TThread("t4", joiner, (void*) 3);
t[4]->Run();
while (!finished) {
for (int i = 0; i < 4; i++) {
if (c[i] && c[i]->IsModified()) {
//printf("Update canvas %d\n", i);
c[i]->Update();
}
}
gSystem->Sleep(100);
}
t[4]->Join();
delete t[0];
delete t[1];
delete t[2];
delete t[3];
delete t[4];
delete rng[0];
delete rng[1];
delete rng[2];
delete rng[3];
}
long
#define c(i)
Definition RSha256.hxx:101
int Int_t
Definition RtypesCore.h:45
const Bool_t kFALSE
Definition RtypesCore.h:101
bool Bool_t
Definition RtypesCore.h:63
long long Long64_t
Definition RtypesCore.h:80
float Float_t
Definition RtypesCore.h:57
const Bool_t kTRUE
Definition RtypesCore.h:100
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:3351
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition TH1.cxx:3074
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:440
virtual Bool_t ProcessEvents()
Process pending events (GUI, timers, sockets).
Definition TSystem.cxx:419
static void Ps()
Static method listing the existing threads.
Definition TThread.cxx:848
static Int_t UnLock()
Static method to unlock the main thread mutex.
Definition TThread.cxx:793
Int_t Kill()
Kill this thread.
Definition TThread.cxx:595
Long_t Join(void **ret=nullptr)
Join this thread.
Definition TThread.cxx:515
Int_t Run(void *arg=nullptr, const int affinity=-1)
Start the thread.
Definition TThread.cxx:571
static Int_t Lock()
Static method to lock the main thread mutex.
Definition TThread.cxx:777
Author
Victor Perevovchikov

Definition in file threadsh1.C.