Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
threads.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 threads.C++.
5/// Before executing the script, load the Thread library with:
6///
7/// ~~~{.cpp}
8/// gSystem->Load("libThread");
9/// ~~~
10///
11/// This is not needed anymore due to the rootmap facility which
12/// automatically loads the needed libraries.
13///
14/// \macro_code
15///
16/// \author Victor Perevovchikov
17
18#include "TThread.h"
19#include <Riostream.h>
20
21void *handle(void *ptr)
22{
23 long nr = (long) ptr;
24
25 for (int i = 0; i < 10; i++) {
26 TThread::Printf("Here I am loop index: %d , thread: %ld", i, nr);
27 gSystem->Sleep(10);
28 }
29 return 0;
30}
31
32void threads()
33{
34
35 gDebug = 1;
36
37 printf("Starting Thread 1\n");
38 TThread *h1 = new TThread("h1", handle, (void*) 1);
39 h1->Run();
40 printf("Starting Thread 2\n");
41 TThread *h2 = new TThread("h2", handle, (void*) 2);
42 h2->Run();
43 printf("Starting Thread 3\n");
44 TThread *h3 = new TThread("h3", handle, (void*) 3);
45 h3->Run();
46
48
49 h1->Join();
51 h2->Join();
52 h3->Join();
54}
Int_t gDebug
Definition TROOT.cxx:597
R__EXTERN TSystem * gSystem
Definition TSystem.h:561
virtual void Sleep(UInt_t milliSec)
Sleep milliSec milli seconds.
Definition TSystem.cxx:437
<div class="legacybox"><h2>Legacy Code</h2> TThread is a legacy interface: there will be no bug fixes...
Definition TThread.h:40
static void Ps()
Static method listing the existing threads.
Definition TThread.cxx:843
static void Printf(const char *fmt,...)
Static method providing a thread safe printf. Appends a newline.
Definition TThread.cxx:916
TH1F * h1
Definition legend1.C:5