ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
threadPool.C
Go to the documentation of this file.
1 // Usage:
2 // root [0] .L threadPool.C++
3 // root [1] threadPool(10) 10 = numThreads
4 
5 // STD
6 #include <iostream>
7 #include <iterator>
8 #include <vector>
9 #ifndef _WIN32
10 #include <unistd.h>
11 #endif
12 // ThreadPool
13 #include "TThreadPool.h"
14 // ROOT
15 #include "TThread.h"
16 
17 //=============================================================================
18 using namespace std;
19 //=============================================================================
20 const size_t g_sleeptime = 1; // in secs.
21 const size_t g_multTasks = 50;
22 //=============================================================================
23 
24 // define a custom parameters type for task objects
25 enum EProc {start, clean};
26 
27 // a class defining task objects
28 class TTestTask: public TThreadPoolTaskImp<TTestTask, EProc>
29 {
30 public:
31  bool runTask(EProc /*_param*/) {
32  m_tid = TThread::SelfId();
34  return true;
35  }
36  unsigned long threadID() const {
37  return m_tid;
38  }
39 
40 private:
41  unsigned long m_tid;
42 };
43 
44 //=============================================================================
45 void threadPool(size_t _numThreads = 10, bool _needDbg = false)
46 {
47  cout << "ThreadPool: starting..." << endl;
48  // number of tasks to process
49  size_t numTasks(_numThreads * g_multTasks);
50 
51  // create a thread pool object
52  // _numThreads - a number of threads in the pool
53  // _needDbg - defines whether to show debug messages
54  TThreadPool<TTestTask, EProc> threadPool(_numThreads, _needDbg);
55 
56  // create a container of tasks
57  vector <TTestTask> tasksList(numTasks);
58 
59  cout << "ThreadPool: getting tasks..." << endl;
60  cout << "ThreadPool: processing tasks..." << endl;
61  // push tasks to the ThreadPool
62  // tasks can be also pushed asynchronously
63  for (size_t i = 0; i < numTasks; ++i) {
64  threadPool.PushTask(tasksList[i], start);
65  }
66 
67  // Stop thread pool.
68  // The parameter "true" requests the calling thread to wait,
69  // until the thread pool task queue is drained.
70  threadPool.Stop(true);
71  cout << "ThreadPool: done" << endl;
72 }
73 
const size_t g_sleeptime
Definition: threadPool.C:20
RooArgList L(const RooAbsArg &v1)
static Long_t SelfId()
Static method returning the id for the current thread.
Definition: TThread.cxx:538
void PushTask(typename TThreadPoolTask< aTask, aParam >::task_t &task, aParam param)
Definition: TThreadPool.h:173
void threadPool(size_t _numThreads=10, bool _needDbg=false)
Definition: threadPool.C:45
void Stop(bool processRemainingJobs=false)
Definition: TThreadPool.h:188
const size_t g_multTasks
Definition: threadPool.C:21
static Int_t Sleep(ULong_t secs, ULong_t nanos=0)
Static method to sleep the calling thread.
Definition: TThread.cxx:736