MarlinMT  0.1.0
marlinmt::concurrency::ThreadPool< IN, OUT > Class Template Reference

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
 
ThreadPooloperator= (const ThreadPool &)=delete
 
ThreadPooloperator= (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 >
 

Detailed Description

template<typename IN, typename OUT>
class marlinmt::concurrency::ThreadPool< 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.

Member Typedef Documentation

◆ Future

template<typename IN, typename OUT>
using marlinmt::concurrency::ThreadPool< IN, OUT >::Future = std::future<OUT>

Definition at line 38 of file ThreadPool.h.

◆ PoolType

template<typename IN, typename OUT>
using marlinmt::concurrency::ThreadPool< IN, OUT >::PoolType = std::vector<std::shared_ptr<Worker<IN,OUT> >>

Definition at line 36 of file ThreadPool.h.

◆ Promise

template<typename IN, typename OUT>
using marlinmt::concurrency::ThreadPool< IN, OUT >::Promise = std::shared_ptr<std::promise<OUT> >

Definition at line 37 of file ThreadPool.h.

◆ PushResult

template<typename IN, typename OUT>
using marlinmt::concurrency::ThreadPool< IN, OUT >::PushResult = std::pair<Promise,Future>

Definition at line 39 of file ThreadPool.h.

◆ QueueType

template<typename IN, typename OUT>
using marlinmt::concurrency::ThreadPool< IN, OUT >::QueueType = Queue<QueueElement<IN,OUT> >

Definition at line 35 of file ThreadPool.h.

Member Enumeration Documentation

◆ PushPolicy

template<typename IN, typename OUT>
enum marlinmt::concurrency::ThreadPool::PushPolicy
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.

Constructor & Destructor Documentation

◆ ThreadPool() [1/3]

template<typename IN, typename OUT>
marlinmt::concurrency::ThreadPool< IN, OUT >::ThreadPool ( )
default

◆ ThreadPool() [2/3]

template<typename IN, typename OUT>
marlinmt::concurrency::ThreadPool< IN, OUT >::ThreadPool ( const ThreadPool< IN, OUT > &  )
delete

◆ ThreadPool() [3/3]

template<typename IN, typename OUT>
marlinmt::concurrency::ThreadPool< IN, OUT >::ThreadPool ( ThreadPool< IN, OUT > &&  )
delete

◆ ~ThreadPool()

template<typename IN , typename OUT >
marlinmt::concurrency::ThreadPool< IN, OUT >::~ThreadPool ( )
inline

Destructor.

Definition at line 184 of file ThreadPool.h.

Member Function Documentation

◆ acceptPush()

template<typename IN , typename OUT >
bool marlinmt::concurrency::ThreadPool< IN, OUT >::acceptPush ( ) const
inline

Whether the thread pool accepts data push.

Definition at line 280 of file ThreadPool.h.

◆ active()

template<typename IN , typename OUT >
bool marlinmt::concurrency::ThreadPool< IN, OUT >::active ( ) const
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().

◆ addWorker()

template<typename IN , typename OUT >
template<typename WORKER , typename ... Args>
void marlinmt::concurrency::ThreadPool< IN, OUT >::addWorker ( Args &&...  args)
inline

Add a new worker thread.

Parameters
argsarguments to pass to worker constructor

Definition at line 192 of file ThreadPool.h.

Referenced by marlinmt::concurrency::PEPScheduler::configurePool().

◆ clearQueue()

template<typename IN , typename OUT >
void marlinmt::concurrency::ThreadPool< IN, OUT >::clearQueue ( )
inline

Clear the queue.

Definition at line 266 of file ThreadPool.h.

◆ freeSlots()

template<typename IN , typename OUT >
std::size_t marlinmt::concurrency::ThreadPool< IN, OUT >::freeSlots ( ) const
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().

◆ isQueueEmpty()

template<typename IN , typename OUT >
bool marlinmt::concurrency::ThreadPool< IN, OUT >::isQueueEmpty ( ) const
inline

Whether the queue is empty.

Definition at line 252 of file ThreadPool.h.

◆ nRunning()

template<typename IN , typename OUT >
std::size_t marlinmt::concurrency::ThreadPool< IN, OUT >::nRunning ( ) const
inline

Get the number of threads currently handling a task.

Definition at line 238 of file ThreadPool.h.

◆ nWaiting()

template<typename IN , typename OUT >
std::size_t marlinmt::concurrency::ThreadPool< IN, OUT >::nWaiting ( ) const
inline

Get the number of waiting threads.

Definition at line 225 of file ThreadPool.h.

Referenced by marlinmt::concurrency::ThreadPool< InputType, OutputType >::nRunning().

◆ operator=() [1/2]

template<typename IN, typename OUT>
ThreadPool& marlinmt::concurrency::ThreadPool< IN, OUT >::operator= ( const ThreadPool< IN, OUT > &  )
delete

◆ operator=() [2/2]

template<typename IN, typename OUT>
ThreadPool& marlinmt::concurrency::ThreadPool< IN, OUT >::operator= ( ThreadPool< IN, OUT > &&  )
delete

◆ push()

template<typename IN, typename OUT >
template<class >
ThreadPool< IN, OUT >::PushResult marlinmt::concurrency::ThreadPool< IN, OUT >::push ( PushPolicy  policy,
IN &&  input 
)
inline

Push a new task in the task queue.

See PushPolicy for runtime behavior of enqueuing.

Parameters
policythe push policy

Definition at line 335 of file ThreadPool.h.

Referenced by marlinmt::concurrency::PEPScheduler::pushEvent().

◆ setAcceptPush()

template<typename IN , typename OUT >
void marlinmt::concurrency::ThreadPool< IN, OUT >::setAcceptPush ( bool  accept)
inline

Set whether the thread pool accept data push.

Parameters
acceptwhether to accept data push

Definition at line 273 of file ThreadPool.h.

Referenced by marlinmt::concurrency::PEPScheduler::configurePool(), and marlinmt::concurrency::PEPScheduler::processRunHeader().

◆ setMaxQueueSize()

template<typename IN , typename OUT >
void marlinmt::concurrency::ThreadPool< IN, OUT >::setMaxQueueSize ( std::size_t  maxQueueSize)
inline

Set the maximum queue size.

Parameters
maxQueueSizethe maximum queue size

Definition at line 259 of file ThreadPool.h.

Referenced by marlinmt::concurrency::PEPScheduler::configurePool().

◆ size()

template<typename IN , typename OUT >
std::size_t marlinmt::concurrency::ThreadPool< IN, OUT >::size ( ) const
inline

Get the thread pool size.

Definition at line 218 of file ThreadPool.h.

◆ start()

template<typename IN , typename OUT >
void marlinmt::concurrency::ThreadPool< IN, OUT >::start ( )
inline

Start the worker threads.

Definition at line 204 of file ThreadPool.h.

Referenced by marlinmt::concurrency::PEPScheduler::configurePool().

◆ stop()

template<typename IN , typename OUT >
void marlinmt::concurrency::ThreadPool< IN, OUT >::stop ( bool  clear = true)
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

Parameters
clearwhether 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().

Friends And Related Function Documentation

◆ Worker< IN, OUT >

template<typename IN, typename OUT>
friend class Worker< IN, OUT >
friend

Definition at line 40 of file ThreadPool.h.

Member Data Documentation

◆ _acceptPush

◆ _conditionVariable

template<typename IN, typename OUT>
std::condition_variable marlinmt::concurrency::ThreadPool< IN, OUT >::_conditionVariable {}
private

◆ _isDone

template<typename IN, typename OUT>
std::atomic<bool> marlinmt::concurrency::ThreadPool< IN, OUT >::_isDone {false}
private

◆ _isRunning

◆ _isStop

template<typename IN, typename OUT>
std::atomic<bool> marlinmt::concurrency::ThreadPool< IN, OUT >::_isStop {false}
private

Whether the thread pool is running.

Definition at line 165 of file ThreadPool.h.

Referenced by marlinmt::concurrency::ThreadPool< InputType, OutputType >::stop().

◆ _mutex

template<typename IN, typename OUT>
std::mutex marlinmt::concurrency::ThreadPool< IN, OUT >::_mutex {}
private

◆ _pool

◆ _queue


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