12 #ifndef ROOT_TThreadPool 13 #define ROOT_TThreadPool 66 template <
class aTask,
class aParam>
69 bool run(aParam ¶m) {
70 aTask *pThis =
reinterpret_cast<aTask *
>(
this);
71 return pThis->runTask(param);
83 template <
class aTask,
class aParam>
94 return fTask.run(fTaskParam);
112 template <
class aTask,
class aParam>
124 fIdleThreads(threadsCount),
128 fAllTasksDone =
new TCondition(&fMutexAllTasksDone);
130 for (
size_t i = 0; i < threadsCount; ++i) {
132 fThreads.push_back(pThread);
140 fThreadMonitor->Run();
147 threads_array_t::const_iterator iter = fThreads.begin();
148 threads_array_t::const_iterator iter_end = fThreads.end();
149 for (; iter != iter_end; ++iter)
152 delete fThreadJoinHelper;
154 delete fThreadNeeded;
155 delete fThreadAvailable;
156 delete fAllTasksDone;
162 fThreads.push_back(pThread);
169 DbgLog(
"Main thread. Try to push a task");
172 task_t *t =
new task_t(task, param);
176 DbgLog(
"Main thread. the task is pushed");
179 fThreadNeeded->Broadcast();
182 void Stop(
bool processRemainingJobs =
false) {
187 if (processRemainingJobs) {
190 while (!fTasks.empty() && !fStopped) {
191 DbgLog(
"Main thread is waiting");
192 fThreadAvailable->Wait();
193 DbgLog(
"Main thread is DONE waiting");
200 fThreadNeeded->Broadcast();
201 DbgLog(
"Main threads requests to STOP");
205 fThreadJoinHelper->Run();
206 fThreadJoinHelper->Join();
213 fAllTasksDone->Wait();
221 return fSuccessfulTasks;
235 std::stringstream ss;
237 <<
">>>> Check for tasks." 238 <<
" Number of Tasks: " << pThis->
fTasks.size()
257 pThis->
DbgLog(
"waiting for a task");
267 pThis->
DbgLog(
"done waiting for tasks");
273 if (!pThis->
fTasks.empty()) {
275 task = pThis->
fTasks.front();
278 pThis->
DbgLog(
"get the task");
283 pThis->
DbgLog(
"done Check <<<<");
288 pThis->
DbgLog(
"Run the task");
300 pThis->
DbgLog(
"Done Running the task");
307 pThis->
DbgLog(
"**** DONE ***");
313 threads_array_t::const_iterator iter = pThis->
fThreads.begin();
314 threads_array_t::const_iterator iter_end = pThis->
fThreads.end();
315 for (; iter != iter_end; ++iter)
TCondition * fThreadNeeded
TThreadPool(size_t threadsCount, bool needDbg=false)
TCondition * fThreadAvailable
TCondition * fAllTasksDone
static void * Executor(void *arg)
TThreadPoolTask< aTask, aParam > task_t
TThreadPoolTaskImp< aTask, aParam > task_t
static Long_t SelfId()
Static method returning the id for the current thread.
Int_t Run(void *arg=0)
Start the thread.
Int_t Wait()
Wait to be signaled.
static void * JoinHelper(void *arg)
size_t TasksCount() const
void PushTask(typename TThreadPoolTask< aTask, aParam >::task_t &task, aParam param)
const TNonCopyable & operator=(const TNonCopyable &)
static bool IsThreadActive(TThread *pThread)
std::vector< TThread * > threads_array_t
std::queue< task_t * > taskqueue_t
size_t IdleThreads() const
static void * Monitor(void *arg)
TThread * fThreadJoinHelper
TMutex fMutexAllTasksDone
void DbgLog(const std::string &msg)
R__EXTERN C unsigned int sleep(unsigned int seconds)
size_t SuccessfulTasks() const
void Stop(bool processRemainingJobs=false)
TThreadPoolTask(task_t &task, aParam ¶m)