MarlinMT
0.1.0
|
ThreadPool class The template parameter T is the type of data to enqueue and process in worker threads. More...
#include <ThreadPool.h>
Public Types | |
enum | PushPolicy { PushPolicy::Blocking, PushPolicy::ThrowIfFull } |
PushPolicy enumerator. More... | |
using | QueueType = Queue< QueueElement< IN, OUT > > |
using | PoolType = std::vector< std::shared_ptr< Worker< IN, OUT > >> |
using | Promise = std::shared_ptr< std::promise< OUT > > |
using | Future = std::future< OUT > |
using | PushResult = std::pair< Promise, Future > |
Public Member Functions | |
ThreadPool ()=default | |
ThreadPool (const ThreadPool &)=delete | |
ThreadPool (ThreadPool &&)=delete | |
ThreadPool & | operator= (const ThreadPool &)=delete |
ThreadPool & | operator= (ThreadPool &&)=delete |
~ThreadPool () | |
Destructor. More... | |
template<typename WORKER , typename ... Args> | |
void | addWorker (Args &&...args) |
Add a new worker thread. More... | |
void | start () |
Start the worker threads. More... | |
std::size_t | size () const |
Get the thread pool size. More... | |
std::size_t | nWaiting () const |
Get the number of waiting threads. More... | |
std::size_t | nRunning () const |
Get the number of threads currently handling a task. More... | |
std::size_t | freeSlots () const |
Get the number of free slots in the task queue. More... | |
bool | isQueueEmpty () const |
Whether the queue is empty. More... | |
void | clearQueue () |
Clear the queue. More... | |
void | setMaxQueueSize (std::size_t maxQueueSize) |
Set the maximum queue size. More... | |
void | setAcceptPush (bool accept) |
Set whether the thread pool accept data push. More... | |
bool | acceptPush () const |
Whether the thread pool accepts data push. More... | |
bool | active () const |
Whether the thread pool is active, meaning that the queue is not empty or at least one worker is active. More... | |
void | stop (bool clear=true) |
Stop the thread pool. More... | |
template<class = typename std::enable_if<not std::is_same<IN,void>::value>::type> | |
PushResult | push (PushPolicy policy, IN &&input) |
Push a new task in the task queue. More... | |
Private Attributes | |
std::mutex | _mutex {} |
< The synchronization mutex More... | |
std::condition_variable | _conditionVariable {} |
The actual thread pool. More... | |
PoolType | _pool {} |
The input element queue. More... | |
QueueType | _queue {} |
Runtime flag... More... | |
std::atomic< bool > | _isDone {false} |
The thread pool stop flag. More... | |
std::atomic< bool > | _isStop {false} |
Whether the thread pool is running. More... | |
std::atomic< bool > | _isRunning {false} |
Whether the thread pool accepts push action. More... | |
std::atomic< bool > | _acceptPush {true} |
Friends | |
class | Worker< IN, OUT > |
ThreadPool class The template parameter T is the type of data to enqueue and process in worker threads.
Definition at line 33 of file ThreadPool.h.
using marlinmt::concurrency::ThreadPool< IN, OUT >::Future = std::future<OUT> |
Definition at line 38 of file ThreadPool.h.
using marlinmt::concurrency::ThreadPool< IN, OUT >::PoolType = std::vector<std::shared_ptr<Worker<IN,OUT> >> |
Definition at line 36 of file ThreadPool.h.
using marlinmt::concurrency::ThreadPool< IN, OUT >::Promise = std::shared_ptr<std::promise<OUT> > |
Definition at line 37 of file ThreadPool.h.
using marlinmt::concurrency::ThreadPool< IN, OUT >::PushResult = std::pair<Promise,Future> |
Definition at line 39 of file ThreadPool.h.
using marlinmt::concurrency::ThreadPool< IN, OUT >::QueueType = Queue<QueueElement<IN,OUT> > |
Definition at line 35 of file ThreadPool.h.
|
strong |
PushPolicy enumerator.
Enumerator | |
---|---|
Blocking | Block until a slot is free in the queue. |
ThrowIfFull | Throw an exception if the queue is full. |
Definition at line 46 of file ThreadPool.h.
|
default |
|
delete |
|
delete |
|
inline |
Destructor.
Definition at line 184 of file ThreadPool.h.
|
inline |
Whether the thread pool accepts data push.
Definition at line 280 of file ThreadPool.h.
|
inline |
Whether the thread pool is active, meaning that the queue is not empty or at least one worker is active.
Definition at line 287 of file ThreadPool.h.
Referenced by marlinmt::concurrency::PEPScheduler::processRunHeader().
|
inline |
Add a new worker thread.
args | arguments to pass to worker constructor |
Definition at line 192 of file ThreadPool.h.
Referenced by marlinmt::concurrency::PEPScheduler::configurePool().
|
inline |
Clear the queue.
Definition at line 266 of file ThreadPool.h.
|
inline |
Get the number of free slots in the task queue.
Definition at line 245 of file ThreadPool.h.
Referenced by marlinmt::concurrency::PEPScheduler::freeSlots().
|
inline |
Whether the queue is empty.
Definition at line 252 of file ThreadPool.h.
|
inline |
Get the number of threads currently handling a task.
Definition at line 238 of file ThreadPool.h.
|
inline |
Get the number of waiting threads.
Definition at line 225 of file ThreadPool.h.
Referenced by marlinmt::concurrency::ThreadPool< InputType, OutputType >::nRunning().
|
delete |
|
delete |
|
inline |
Push a new task in the task queue.
See PushPolicy for runtime behavior of enqueuing.
policy | the push policy |
Definition at line 335 of file ThreadPool.h.
Referenced by marlinmt::concurrency::PEPScheduler::pushEvent().
|
inline |
Set whether the thread pool accept data push.
accept | whether to accept data push |
Definition at line 273 of file ThreadPool.h.
Referenced by marlinmt::concurrency::PEPScheduler::configurePool(), and marlinmt::concurrency::PEPScheduler::processRunHeader().
|
inline |
Set the maximum queue size.
maxQueueSize | the maximum queue size |
Definition at line 259 of file ThreadPool.h.
Referenced by marlinmt::concurrency::PEPScheduler::configurePool().
|
inline |
Get the thread pool size.
Definition at line 218 of file ThreadPool.h.
|
inline |
Start the worker threads.
Definition at line 204 of file ThreadPool.h.
Referenced by marlinmt::concurrency::PEPScheduler::configurePool().
|
inline |
Stop the thread pool.
If the flag clear is set to true, the task queue is cleared. The threads are joined and the pool is clear. As no threads remains in the pool, the pool is not reusable. Thus this method must be called for performing a proper program termination before exiting
clear | whether the task queue should be cleared |
Definition at line 302 of file ThreadPool.h.
Referenced by marlinmt::concurrency::PEPScheduler::end(), and marlinmt::concurrency::ThreadPool< InputType, OutputType >::~ThreadPool().
|
friend |
Definition at line 40 of file ThreadPool.h.
|
private |
|
private |
The actual thread pool.
Definition at line 157 of file ThreadPool.h.
Referenced by marlinmt::concurrency::ThreadPool< InputType, OutputType >::push(), marlinmt::concurrency::Worker< IN, OUT >::run(), and marlinmt::concurrency::ThreadPool< InputType, OutputType >::stop().
|
private |
The thread pool stop flag.
Definition at line 163 of file ThreadPool.h.
Referenced by marlinmt::concurrency::Worker< IN, OUT >::run(), and marlinmt::concurrency::ThreadPool< InputType, OutputType >::stop().
|
private |
Whether the thread pool accepts push action.
Definition at line 167 of file ThreadPool.h.
Referenced by marlinmt::concurrency::ThreadPool< InputType, OutputType >::addWorker(), marlinmt::concurrency::ThreadPool< InputType, OutputType >::push(), marlinmt::concurrency::ThreadPool< InputType, OutputType >::start(), and marlinmt::concurrency::ThreadPool< InputType, OutputType >::stop().
|
private |
Whether the thread pool is running.
Definition at line 165 of file ThreadPool.h.
Referenced by marlinmt::concurrency::ThreadPool< InputType, OutputType >::stop().
|
private |
< The synchronization mutex
The queue enqueuing condition variable
Definition at line 155 of file ThreadPool.h.
Referenced by marlinmt::concurrency::ThreadPool< InputType, OutputType >::push(), marlinmt::concurrency::Worker< IN, OUT >::run(), and marlinmt::concurrency::ThreadPool< InputType, OutputType >::stop().
|
private |
The input element queue.
Definition at line 159 of file ThreadPool.h.
Referenced by marlinmt::concurrency::ThreadPool< InputType, OutputType >::active(), marlinmt::concurrency::ThreadPool< InputType, OutputType >::addWorker(), marlinmt::concurrency::ThreadPool< InputType, OutputType >::nRunning(), marlinmt::concurrency::ThreadPool< InputType, OutputType >::nWaiting(), marlinmt::concurrency::ThreadPool< InputType, OutputType >::size(), marlinmt::concurrency::ThreadPool< InputType, OutputType >::start(), and marlinmt::concurrency::ThreadPool< InputType, OutputType >::stop().
|
private |
Runtime flag...
Definition at line 161 of file ThreadPool.h.
Referenced by marlinmt::concurrency::ThreadPool< InputType, OutputType >::active(), marlinmt::concurrency::ThreadPool< InputType, OutputType >::clearQueue(), marlinmt::concurrency::ThreadPool< InputType, OutputType >::freeSlots(), marlinmt::concurrency::ThreadPool< InputType, OutputType >::isQueueEmpty(), marlinmt::concurrency::ThreadPool< InputType, OutputType >::push(), marlinmt::concurrency::Worker< IN, OUT >::run(), marlinmt::concurrency::ThreadPool< InputType, OutputType >::setMaxQueueSize(), and marlinmt::concurrency::ThreadPool< InputType, OutputType >::stop().