ROOT logo
//+ 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");
// This is not needed anymore due to the rootmap facility which
// automatically loads the needed libraries.
//Author: Victor Perevovchikov
   
#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: %3d , thread: %lld", i, nr);
      gSystem->Sleep(1000);
   }
   return 0;
}

void threads()
{
#ifdef __CINT__
   printf("This script can only be executed via ACliC: .x threads.C++\n");
   return;
#endif

   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();

   TThread::Ps();

   h1->Join();
   TThread::Ps();
   h2->Join();
   h3->Join();
   TThread::Ps();
}
 threads.C:1
 threads.C:2
 threads.C:3
 threads.C:4
 threads.C:5
 threads.C:6
 threads.C:7
 threads.C:8
 threads.C:9
 threads.C:10
 threads.C:11
 threads.C:12
 threads.C:13
 threads.C:14
 threads.C:15
 threads.C:16
 threads.C:17
 threads.C:18
 threads.C:19
 threads.C:20
 threads.C:21
 threads.C:22
 threads.C:23
 threads.C:24
 threads.C:25
 threads.C:26
 threads.C:27
 threads.C:28
 threads.C:29
 threads.C:30
 threads.C:31
 threads.C:32
 threads.C:33
 threads.C:34
 threads.C:35
 threads.C:36
 threads.C:37
 threads.C:38
 threads.C:39
 threads.C:40
 threads.C:41
 threads.C:42
 threads.C:43
 threads.C:44
 threads.C:45
 threads.C:46
 threads.C:47
 threads.C:48
 threads.C:49
 threads.C:50
 threads.C:51
 threads.C:52
 threads.C:53
 threads.C:54