12 #ifndef ROOT_TThreadPool 13 #define ROOT_TThreadPool 33 #define sleep(s) _sleep(s) 69 template <
class aTask,
class aParam>
72 bool run(aParam ¶m) {
73 aTask *pThis =
reinterpret_cast<aTask *
>(
this);
74 return pThis->runTask(param);
86 template <
class aTask,
class aParam>
97 return fTask.run(fTaskParam);
115 template <
class aTask,
class aParam>
127 fIdleThreads(threadsCount),
131 fAllTasksDone =
new TCondition(&fMutexAllTasksDone);
133 for (
size_t i = 0; i < threadsCount; ++i) {
135 fThreads.push_back(pThread);
143 fThreadMonitor->Run();
150 threads_array_t::const_iterator iter = fThreads.begin();
151 threads_array_t::const_iterator iter_end = fThreads.end();
152 for (; iter != iter_end; ++iter)
155 delete fThreadJoinHelper;
157 delete fThreadNeeded;
158 delete fThreadAvailable;
159 delete fAllTasksDone;
165 fThreads.push_back(pThread);
172 DbgLog(
"Main thread. Try to push a task");
175 task_t *t =
new task_t(task, param);
179 DbgLog(
"Main thread. the task is pushed");
182 fThreadNeeded->Broadcast();
185 void Stop(
bool processRemainingJobs =
false) {
190 if (processRemainingJobs) {
193 while (!fTasks.empty() && !fStopped) {
194 DbgLog(
"Main thread is waiting");
195 fThreadAvailable->Wait();
196 DbgLog(
"Main thread is DONE waiting");
203 fThreadNeeded->Broadcast();
204 DbgLog(
"Main threads requests to STOP");
208 fThreadJoinHelper->Run();
209 fThreadJoinHelper->Join();
216 fAllTasksDone->Wait();
224 return fSuccessfulTasks;
238 std::stringstream ss;
240 <<
">>>> Check for tasks." 241 <<
" Number of Tasks: " << pThis->
fTasks.size()
260 pThis->
DbgLog(
"waiting for a task");
270 pThis->
DbgLog(
"done waiting for tasks");
276 if (!pThis->
fTasks.empty()) {
278 task = pThis->
fTasks.front();
281 pThis->
DbgLog(
"get the task");
286 pThis->
DbgLog(
"done Check <<<<");
291 pThis->
DbgLog(
"Run the task");
303 pThis->
DbgLog(
"Done Running the task");
310 pThis->
DbgLog(
"**** DONE ***");
316 threads_array_t::const_iterator iter = pThis->
fThreads.begin();
317 threads_array_t::const_iterator iter_end = pThis->
fThreads.end();
318 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)