Logo ROOT   6.16/01
Reference Guide
threads.C File Reference

Detailed Description

Example of a simple script creating 3 threads.

This script can only be executed via ACliC: .x threads.C++. Before executing the script, load the Thread library with:

gSystem->Load("libThread");
R__EXTERN TSystem * gSystem
Definition: TSystem.h:540
virtual int Load(const char *module, const char *entry="", Bool_t system=kFALSE)
Load a shared library.
Definition: TSystem.cxx:1843

This is not needed anymore due to the rootmap facility which automatically loads the needed libraries.

#include "TThread.h"
#include <Riostream.h>
void *handle(void *ptr)
{
long nr = (long) ptr;
for (int i = 0; i < 10; i++) {
//TThread::Lock();
//printf("Here I am loop index: %3d , thread: %d\n",i,nr);
//TThread::UnLock();
TThread::Printf("Here I am loop index: %d , thread: %ld", i, nr);
gSystem->Sleep(1000);
}
return 0;
}
void threads()
{
gDebug = 1;
printf("Starting Thread 1\n");
TThread *h1 = new TThread("h1", handle, (void*) 1);
h1->Run();
printf("Starting Thread 2\n");
TThread *h2 = new TThread("h2", handle, (void*) 2);
h2->Run();
printf("Starting Thread 3\n");
TThread *h3 = new TThread("h3", handle, (void*) 3);
h3->Run();
h1->Join();
h2->Join();
h3->Join();
}
R__EXTERN Int_t gDebug
Definition: Rtypes.h:90
virtual void Sleep(UInt_t milliSec)
Sleep milliSec milli seconds.
Definition: TSystem.cxx:446
static void Ps()
Static method listing the existing threads.
Definition: TThread.cxx:838
Long_t Join(void **ret=0)
Join this thread.
Definition: TThread.cxx:508
static void Printf(const char *fmt,...)
Static method providing a thread safe printf. Appends a newline.
Definition: TThread.cxx:919
Int_t Run(void *arg=0)
Start the thread.
Definition: TThread.cxx:561
TH1F * h1
Definition: legend1.C:5
Author
Victor Perevovchikov

Definition in file threads.C.