Helios++
Helios software for LiDAR simulations
WarehouseThreadPool< Task > Class Template Reference

Thread pool which starts thread so they are always waiting for new tasks to be posted to the warehouse to compute them. More...

#include <WarehouseThreadPool.h>

Inheritance diagram for WarehouseThreadPool< Task >:
Collaboration diagram for WarehouseThreadPool< Task >:

Public Member Functions

virtual bool post (shared_ptr< Task > task)
 Expose the warehouse post method. More...
 
virtual bool post (vector< shared_ptr< Task >> &tasks)
 Expose the warehouse post method. More...
 
virtual shared_ptr< Task > get ()
 Expose the warehouse get method. More...
 
virtual void notify ()
 Expose the warehouse notify method. More...
 
virtual void notifyAll ()
 Expose the warehouse notify all method. More...
 
virtual void start ()
 Start the warehouse thread pool. More...
 
virtual void join ()
 Lock until warehouse is empty.
 
virtual void finish ()
 Finish the warehouse thread pool in a proper way. It is, allowing all tasks to finish properly and emptying the warehouse. More...
 
- Public Member Functions inherited from ThreadPool
 ThreadPool (std::size_t const _pool_size)
 Thread pool constructor. More...
 
virtual std::size_t getPoolSize () const
 Obtain the thread pool size. More...
 

Protected Member Functions

 WarehouseThreadPool (std::size_t const _pool_size, std::size_t const maxTasks=256)
 Warehouse thread pool constructor. More...
 
virtual void _start (size_t const tid)
 Start a thread. More...
 
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. To handle tasks with arguments extend the WarehouseThreadPool and override this method accordingly. More...
 
virtual void finalJoin ()
 Lock until all pending tasks have been finished. If it is not called after finish, it will lead to an eternal waiting. More...
 

Protected Attributes

TaskWarehouse< Task > warehouse
 The task warehouse used to handle tasks.
 
bool working
 True if thread pool is working, false if it is already finished. More...
 
int workersCount
 Count how many active workers there are. It is used for final join.
 
int pendingCount
 Count how many workers with pending tasks there are. It is used for join (not final join). It can also be read as working count (not workers count) because it counts how many working threads there are.
 
boost::mutex mtx
 Mutex to handle concurrent access to workers count.
 
boost::condition_variable condvar
 Conditional variable to handle concurrent access to workers count.
 
boost::mutex joinMtx
 Mutex to handle join until warehouse is empty (not the final join) More...
 
boost::condition_variable joinCondvar
 Conditional variable to handle join until warehouse is empty (not the final join) More...
 
boost::asio::io_service io_service_
 Instance of boost input/output service for asynchronous data processing.
 
std::size_t pool_size
 Size of thread pool (number of threads)
 
- Protected Attributes inherited from ThreadPool
boost::asio::io_service io_service_
 Instance of boost input/output service for asynchronous data processing.
 
boost::asio::executor_work_guard< boost::asio::io_service::executor_type > work_
 Instance of work guard to report the io service when it has pending tasks. More...
 
std::size_t pool_size
 Size of thread pool (number of threads)
 
boost::thread_group threads_
 Group of threads.
 

Detailed Description

template<typename Task>
class WarehouseThreadPool< Task >

Thread pool which starts thread so they are always waiting for new tasks to be posted to the warehouse to compute them.

Author
Alberto M. Esmoris Pena
Version
1.0
Template Parameters
TaskThe type of task handled by the warehouse thread pool

Constructor & Destructor Documentation

◆ WarehouseThreadPool()

template<typename Task >
WarehouseThreadPool< Task >::WarehouseThreadPool ( std::size_t const  _pool_size,
std::size_t const  maxTasks = 256 
)
inlineexplicitprotected

Warehouse thread pool constructor.

See also
ThreadPool::pool_size
ThreadPool::ThreadPool

Member Function Documentation

◆ _start()

template<typename Task >
virtual void WarehouseThreadPool< Task >::_start ( size_t const  tid)
inlineprotectedvirtual

Start a thread.

Started threads compute tasks from warehouse. If there are not remaining tasks they wait for a notify signal to resume its activity, to avoid active listening. To stop threads the WarehouseThreadPool::finish method must be invoked.

Parameters
tidIndex for thread to be started
See also
WarehouseThreadPool::warehouse
WarehouseThreadPool::finish
WarehouseThreadPool::start

◆ doTask()

template<typename Task >
virtual void WarehouseThreadPool< Task >::doTask ( size_t const  tid,
shared_ptr< Task >  task 
)
inlineprotectedvirtual

Thread execute given task. By default it assumes task must be called with no arguments. To handle tasks with arguments extend the WarehouseThreadPool and override this method accordingly.

Parameters
tidIndex/identifier of thread that must compute the task
taskTask to be computed

Reimplemented in PulseWarehouseThreadPool.

◆ finalJoin()

template<typename Task >
virtual void WarehouseThreadPool< Task >::finalJoin ( )
inlineprotectedvirtual

Lock until all pending tasks have been finished. If it is not called after finish, it will lead to an eternal waiting.

See also
WarehouseThreadPool::finish

◆ finish()

template<typename Task >
virtual void WarehouseThreadPool< Task >::finish ( )
inlinevirtual

Finish the warehouse thread pool in a proper way. It is, allowing all tasks to finish properly and emptying the warehouse.

See also
WarehouseThreadPool::finalJoin

◆ get()

template<typename Task >
virtual shared_ptr<Task> WarehouseThreadPool< Task >::get ( )
inlinevirtual

Expose the warehouse get method.

See also
TaskWarehouse::get

◆ notify()

template<typename Task >
virtual void WarehouseThreadPool< Task >::notify ( )
inlinevirtual

Expose the warehouse notify method.

See also
TaskWarehouse::notify

◆ notifyAll()

template<typename Task >
virtual void WarehouseThreadPool< Task >::notifyAll ( )
inlinevirtual

Expose the warehouse notify all method.

See also
TaskWarehouse::notifyAll

◆ post() [1/2]

template<typename Task >
virtual bool WarehouseThreadPool< Task >::post ( shared_ptr< Task >  task)
inlinevirtual

Expose the warehouse post method.

See also
TaskWarehouse::post(shared_ptr<Task>)

◆ post() [2/2]

template<typename Task >
virtual bool WarehouseThreadPool< Task >::post ( vector< shared_ptr< Task >> &  tasks)
inlinevirtual

Expose the warehouse post method.

See also
TaskWarehouse::post(vector<shared_ptr<Task>>&)

◆ start()

template<typename Task >
virtual void WarehouseThreadPool< Task >::start ( )
inlinevirtual

Start the warehouse thread pool.

A non started warehouse thread pool will not compute any posted task until it is started.

See also
WarehouseThreadPool::_start

Member Data Documentation

◆ joinCondvar

template<typename Task >
boost::condition_variable WarehouseThreadPool< Task >::joinCondvar
protected

Conditional variable to handle join until warehouse is empty (not the final join)

See also
WarehouseThreadPool::join
WarehouseThreadPool::finalJoin

◆ joinMtx

template<typename Task >
boost::mutex WarehouseThreadPool< Task >::joinMtx
protected

Mutex to handle join until warehouse is empty (not the final join)

See also
WarehouseThreadPool::join
WarehouseThreadPool::finalJoin

◆ working

template<typename Task >
bool WarehouseThreadPool< Task >::working
protected

True if thread pool is working, false if it is already finished.

NOTICE that if working is true threads are guaranteed to be started and not finished. In consequence, threads might be computing tasks or waiting for new tasks. A warehouse thread pool with all threads waiting for tasks is considered as working. A warehouse thread pool is not working only if it has finished accepting tasks.


The documentation for this class was generated from the following file: