Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
stressThreadPool.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_thread
3///
4/// Usage:
5///
6/// ~~~{.cpp}
7/// root [0] .L stressThreadPool.C++
8/// root [1] stressThreadPool(5, true)
9/// ~~~
10///
11/// where 5 is a number of Threads in the pool
12/// there will be then nThreads * 10 tasks pushed to the test
13///
14/// \macro_code
15///
16/// \author Victor Perevovchikov
17
18// STD
19#include <iostream>
20#include <iterator>
21#include <vector>
22#ifndef _WIN32
23#include <unistd.h>
24#endif
25// ThreadPool
26#include "TThreadPool.h"
27// ROOT
28#include "TThread.h"
29
30//=============================================================================
31using std::ostream, std::map, std::cout, std::endl;
32//=============================================================================
33const size_t g_sleeptime = 10000000; // in nanosecs.
34const size_t g_multTasks = 8;
35//=============================================================================
36
37enum EProc {start, clean};
38
39class TTestTask: public TThreadPoolTaskImp<TTestTask, EProc> {
40public:
41 bool runTask(EProc /*_param*/) {
42 m_tid = TThread::SelfId();
43 TThread::Sleep(0L, g_sleeptime);
44 return true;
45 }
46 unsigned long threadID() const {
47 return m_tid;
48 }
49
50private:
51 unsigned long m_tid;
52};
53ostream &operator<< (ostream &_stream, const TTestTask &_task)
54{
55 _stream << _task.threadID();
56 return _stream;
57}
58
59//=============================================================================
60void stressThreadPool(size_t _numThreads = 4, bool _needDbg = false)
61{
62 size_t numTasks(_numThreads * g_multTasks);
63 TThreadPool<TTestTask, EProc> threadPool(_numThreads, _needDbg);
64 vector <TTestTask> tasksList(numTasks);
65 // Pushing 4 * numTasks task in the pool
66 // We want to dain the task queue before pushing a next bunch of tasks (just to show you a Drain method ;) )
67 for (size_t j = 0; j < 4; ++j )
68 {
69 cout << "+++++++++ Starting iteration #" << j << " ++++++++++++"<< endl;
70 for (size_t i = 0; i < numTasks; ++i) {
71 threadPool.PushTask(tasksList[i], start);
72 }
73
74 cout << "\n ****** Drain the tasks queue ******" << endl;
75 threadPool.Drain();
76 }
77 cout << "\n Stopping..." << endl;
78 threadPool.Stop(true);
79
80 // ostream_iterator<TTestTask> out_it( cout, "\n" );
81 // copy( tasksList.begin(), tasksList.end(),
82 // out_it );
83
84 typedef map<unsigned long, size_t> counter_t;
85 counter_t counter;
86 {
87 vector <TTestTask>::const_iterator iter = tasksList.begin();
88 vector <TTestTask>::const_iterator iter_end = tasksList.end();
89 for (; iter != iter_end; ++iter) {
90 counter_t::iterator found = counter.find(iter->threadID());
91 if (found == counter.end())
92 counter.insert(counter_t::value_type(iter->threadID(), 1));
93 else {
94 found->second = found->second + 1;
95 }
96 }
97 }
98
99 cout << "\n************* RESULT ****************" << endl;
100
101 counter_t::const_iterator iter = counter.begin();
102 counter_t::const_iterator iter_end = counter.end();
103 bool testOK = true;
104 for (; iter != iter_end; ++iter) {
105 cout << "Thread " << iter->first << " was used " << iter->second << " times\n";
106 // each thread suppose to be used equal amount of time,
107 // exactly (g_numTasks/g_numThreads) times
108 if (iter->second != g_multTasks)
109 testOK = false;
110 }
111
112 cout << "ThreadPool: the simple test status: " << (testOK ? "OK" : "Failed") << endl;
113}
static Int_t Sleep(ULong_t secs, ULong_t nanos=0)
Static method to sleep the calling thread.
Definition TThread.cxx:750
static Long_t SelfId()
Static method returning the id for the current thread.
Definition TThread.cxx:549