12#ifndef WSFTHREADPOOL_HPP
13#define WSFTHREADPOOL_HPP
38template<
class Worker,
class WorkerFactory = WsfThreadPool_DefaultWorkerFactor<Worker>>
47 void Start(
unsigned int aNumThreads);
59 WorkerFactory mFactory;
63template<
class Worker,
class WorkerFactory>
69template<
class Worker,
class WorkerFactory>
76 for (
unsigned int index = 0; index < mThread_vector.size(); ++index)
78 delete mThread_vector[index];
80 mThread_vector.clear();
83template<
class Worker,
class WorkerFactory>
87 if (mThread_vector.empty())
90 mThread_vector.reserve(aNumThreads);
91 for (
unsigned int count = 1; count <= aNumThreads; ++count)
93 Worker* workerPtr = mFactory();
95 mThread_vector.push_back(workerPtr);
100template<
class Worker,
class WorkerFactory>
104 UtThread::UtThreads tempThreads;
105 for (
unsigned int index = 0; index < mThread_vector.size(); ++index)
107 mThread_vector[index]->StopWork();
108 mThread_vector[index]->EndThread();
109 tempThreads.push_back(mThread_vector[index]);
113 if (!tempThreads.empty())
115 UtThread::JoinAll(tempThreads);
119template<
class Worker,
class WorkerFactory>
123 bool startedWork(
false);
124 for (
auto* threadPtr : mThread_vector)
126 if (threadPtr->AssignWork())
139template<
class Worker,
class WorkerFactory>
143 Worker* availableThreadPtr = 0;
144 unsigned int threadPoolSize(mThread_vector.size());
145 for (
unsigned int index = 0; index < threadPoolSize; ++index)
149 availableThreadPtr = mThread_vector[index];
155 if (availableThreadPtr == 0)
157 UtSleep::Sleep(aSleepTime);
160 return availableThreadPtr;
163template<
class Worker,
class WorkerFactory>
166 bool atLeastOneStillWorking(
false);
167 size_t threadPoolSize(mThread_vector.size());
168 for (
size_t index = 0; index < threadPoolSize; ++index)
172 atLeastOneStillWorking =
true;
176 return (!atLeastOneStillWorking);
179template<
class Worker,
class WorkerFactory>
182 for (
auto threadPtr : mThread_vector)
184 threadPtr->WaitUntilWorkDone();
188template<
class Worker,
class WorkerFactory>
191 auto start = std::chrono::system_clock::now();
192 for (
auto threadPtr : mThread_vector)
194 if (!threadPtr->TryWaitUntilWorkDone(aSecsToWait))
198 auto now = std::chrono::system_clock::now();
199 using secs_t = std::chrono::duration<double>;
200 aSecsToWait -= secs_t{now - start}.count();
void Stop()
Definition WsfThreadPool.hpp:101
void Start(unsigned int aNumThreads)
Definition WsfThreadPool.hpp:84
bool AssignWork(size_t aNumTasks=0u)
Definition WsfThreadPool.hpp:120
WsfThreadPool(const WorkerFactory &aFactory=WorkerFactory())
Definition WsfThreadPool.hpp:64
void WaitUntilAllWorkDone()
Definition WsfThreadPool.hpp:180
std::vector< Worker * > WorkerThreads
Definition WsfThreadPool.hpp:42
bool TryWaitUntilAllWorkDone(double aSecsToWait)
Definition WsfThreadPool.hpp:189
~WsfThreadPool()
Definition WsfThreadPool.hpp:70
bool AllWorkDone()
Definition WsfThreadPool.hpp:164
Worker * FindAvailableThread(double aSleepTime=0.001)
Definition WsfThreadPool.hpp:140
@ AVAILABLE
Definition WsfThread.hpp:28
Definition WsfThreadPool.hpp:33
T * operator()() const
Definition WsfThreadPool.hpp:34