3 #include <ThreadPool.h>
4 #include <TaskWarehouse.h>
17 template <
typename Task>
82 std::size_t
const _pool_size,
83 std::size_t
const maxTasks=256
100 virtual inline bool post(shared_ptr<Task> task)
106 virtual inline bool post(vector<shared_ptr<Task>> &tasks)
112 virtual inline shared_ptr<Task>
get()
137 for(
size_t tid = 0 ; tid <
pool_size ; ++tid){
152 boost::unique_lock<boost::mutex> lock(
joinMtx);
183 shared_ptr<Task> task;
187 boost::unique_lock<boost::mutex> lockIncrease(
joinMtx);
189 lockIncrease.unlock();
190 while( (task=
warehouse.get()) !=
nullptr){
193 boost::unique_lock<boost::mutex> lockDecrease(
joinMtx);
195 lockDecrease.unlock();
202 boost::unique_lock<boost::mutex> lockIncrease(
joinMtx);
204 lockIncrease.unlock();
205 while( (task=
warehouse.get()) !=
nullptr){
208 boost::unique_lock<boost::mutex> lockDecrease(
joinMtx);
210 lockDecrease.unlock();
215 boost::unique_lock<boost::mutex> lock(
mtx);
228 virtual void doTask(
size_t const tid, shared_ptr<Task> task){
238 boost::unique_lock<boost::mutex> lock(
mtx);
Class implementing a warehouse to store and retrieve tasks in a thread safe fashion.
Definition: TaskWarehouse.h:22
Base class providing core implementation of a thread pool to deal with multi threading tasks.
Definition: ThreadPool.h:28
std::size_t pool_size
Size of thread pool (number of threads)
Definition: ThreadPool.h:47
boost::asio::io_service io_service_
Instance of boost input/output service for asynchronous data processing.
Definition: ThreadPool.h:36
Thread pool which starts thread so they are always waiting for new tasks to be posted to the warehous...
Definition: WarehouseThreadPool.h:18
virtual void finalJoin()
Lock until all pending tasks have been finished. If it is not called after finish,...
Definition: WarehouseThreadPool.h:237
std::size_t pool_size
Size of thread pool (number of threads)
Definition: ThreadPool.h:47
virtual void finish()
Finish the warehouse thread pool in a proper way. It is, allowing all tasks to finish properly and em...
Definition: WarehouseThreadPool.h:163
boost::mutex mtx
Mutex to handle concurrent access to workers count.
Definition: WarehouseThreadPool.h:52
boost::asio::io_service io_service_
Instance of boost input/output service for asynchronous data processing.
Definition: ThreadPool.h:36
boost::mutex joinMtx
Mutex to handle join until warehouse is empty (not the final join)
Definition: WarehouseThreadPool.h:64
virtual void notify()
Expose the warehouse notify method.
Definition: WarehouseThreadPool.h:118
virtual void join()
Lock until warehouse is empty.
Definition: WarehouseThreadPool.h:151
virtual void start()
Start the warehouse thread pool.
Definition: WarehouseThreadPool.h:134
WarehouseThreadPool(std::size_t const _pool_size, std::size_t const maxTasks=256)
Warehouse thread pool constructor.
Definition: WarehouseThreadPool.h:81
virtual bool post(vector< shared_ptr< Task >> &tasks)
Expose the warehouse post method.
Definition: WarehouseThreadPool.h:106
boost::condition_variable condvar
Conditional variable to handle concurrent access to workers count.
Definition: WarehouseThreadPool.h:57
TaskWarehouse< Task > warehouse
The task warehouse used to handle tasks.
Definition: WarehouseThreadPool.h:27
boost::condition_variable joinCondvar
Conditional variable to handle join until warehouse is empty (not the final join)
Definition: WarehouseThreadPool.h:71
bool working
True if thread pool is working, false if it is already finished.
Definition: WarehouseThreadPool.h:37
int pendingCount
Count how many workers with pending tasks there are. It is used for join (not final join)....
Definition: WarehouseThreadPool.h:48
virtual bool post(shared_ptr< Task > task)
Expose the warehouse post method.
Definition: WarehouseThreadPool.h:100
virtual void notifyAll()
Expose the warehouse notify all method.
Definition: WarehouseThreadPool.h:124
virtual void _start(size_t const tid)
Start a thread.
Definition: WarehouseThreadPool.h:182
int workersCount
Count how many active workers there are. It is used for final join.
Definition: WarehouseThreadPool.h:42
virtual shared_ptr< Task > get()
Expose the warehouse get method.
Definition: WarehouseThreadPool.h:112
virtual void doTask(size_t const tid, shared_ptr< Task > task)
Thread execute given task. By default it assumes task must be called with no arguments....
Definition: WarehouseThreadPool.h:228