12 #ifndef ROOT_TThreadPool 13 #define ROOT_TThreadPool 29 #ifndef ROOT_TCondition 72 template <
class aTask,
class aParam>
75 bool run(aParam ¶m) {
76 aTask *pThis =
reinterpret_cast<aTask *
>(
this);
77 return pThis->runTask(param);
89 template <
class aTask,
class aParam>
100 return fTask.run(fTaskParam);
118 template <
class aTask,
class aParam>
130 fIdleThreads(threadsCount),
134 fAllTasksDone =
new TCondition(&fMutexAllTasksDone);
136 for (
size_t i = 0; i < threadsCount; ++i) {
138 fThreads.push_back(pThread);
146 fThreadMonitor->Run();
153 threads_array_t::const_iterator iter = fThreads.begin();
154 threads_array_t::const_iterator iter_end = fThreads.end();
155 for (; iter != iter_end; ++iter)
158 delete fThreadJoinHelper;
160 delete fThreadNeeded;
161 delete fThreadAvailable;
162 delete fAllTasksDone;
168 fThreads.push_back(pThread);
175 DbgLog(
"Main thread. Try to push a task");
178 task_t *t =
new task_t(task, param);
182 DbgLog(
"Main thread. the task is pushed");
185 fThreadNeeded->Broadcast();
188 void Stop(
bool processRemainingJobs =
false) {
193 if (processRemainingJobs) {
196 while (!fTasks.empty() && !fStopped) {
197 DbgLog(
"Main thread is waiting");
198 fThreadAvailable->Wait();
199 DbgLog(
"Main thread is DONE waiting");
206 fThreadNeeded->Broadcast();
207 DbgLog(
"Main threads requests to STOP");
211 fThreadJoinHelper->Run();
212 fThreadJoinHelper->Join();
219 fAllTasksDone->Wait();
227 return fSuccessfulTasks;
241 std::stringstream ss;
243 <<
">>>> Check for tasks." 244 <<
" Number of Tasks: " << pThis->
fTasks.size()
263 pThis->
DbgLog(
"waiting for a task");
273 pThis->
DbgLog(
"done waiting for tasks");
279 if (!pThis->
fTasks.empty()) {
281 task = pThis->
fTasks.front();
284 pThis->
DbgLog(
"get the task");
289 pThis->
DbgLog(
"done Check <<<<");
294 pThis->
DbgLog(
"Run the task");
306 pThis->
DbgLog(
"Done Running the task");
313 pThis->
DbgLog(
"**** DONE ***");
319 threads_array_t::const_iterator iter = pThis->
fThreads.begin();
320 threads_array_t::const_iterator iter_end = pThis->
fThreads.end();
321 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)