ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
threads.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 threads.C++.
3 // Before executing the script, load the Thread library with:
4 // gSystem->Load("libThread");
5 // This is not needed anymore due to the rootmap facility which
6 // automatically loads the needed libraries.
7 //Author: Victor Perevovchikov
8 
9 #include "TThread.h"
10 #include <Riostream.h>
11 
12 void *handle(void *ptr)
13 {
14  long nr = (long) ptr;
15 
16  for (int i = 0; i < 10; i++) {
17  //TThread::Lock();
18  //printf("Here I am loop index: %3d , thread: %d\n",i,nr);
19  //TThread::UnLock();
20 
21  TThread::Printf("Here I am loop index: %d , thread: %ld", i, nr);
22  gSystem->Sleep(1000);
23  }
24  return 0;
25 }
26 
27 void threads()
28 {
29 #ifdef __CINT__
30  printf("This script can only be executed via ACliC: .x threads.C++\n");
31  return;
32 #endif
33 
34  gDebug = 1;
35 
36  printf("Starting Thread 1\n");
37  TThread *h1 = new TThread("h1", handle, (void*) 1);
38  h1->Run();
39  printf("Starting Thread 2\n");
40  TThread *h2 = new TThread("h2", handle, (void*) 2);
41  h2->Run();
42  printf("Starting Thread 3\n");
43  TThread *h3 = new TThread("h3", handle, (void*) 3);
44  h3->Run();
45 
46  TThread::Ps();
47 
48  h1->Join();
49  TThread::Ps();
50  h2->Join();
51  h3->Join();
52  TThread::Ps();
53 }
void threads()
Definition: threads.C:27
static void Printf(const char *fmt,...)
Static method providing a thread safe printf. Appends a newline.
Definition: TThread.cxx:910
virtual void Sleep(UInt_t milliSec)
Sleep milliSec milli seconds.
Definition: TSystem.cxx:441
TH2D * h2
Definition: fit2dHist.C:45
TH1F * h1
Definition: legend1.C:5
Int_t Run(void *arg=0)
Start the thread.
Definition: TThread.cxx:552
R__EXTERN TSystem * gSystem
Definition: TSystem.h:545
Long_t Join(void **ret=0)
Join this thread.
Definition: TThread.cxx:499
static void Ps()
Static method listing the existing threads.
Definition: TThread.cxx:829
ClassImp(TMCParticle) void TMCParticle printf(": p=(%7.3f,%7.3f,%9.3f) ;", fPx, fPy, fPz)
void * handle(void *ptr)
Definition: threads.C:12
R__EXTERN Int_t gDebug
Definition: Rtypes.h:128