TwiceAsNice  2019-02-18
Classes | Public Member Functions | Protected Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
Nice::DispatcherThreadPool< Task, SchedulingPolicy > Class Template Reference

Thread pool. More...

#include <DispatcherThreadPool.hh>

Inheritance diagram for Nice::DispatcherThreadPool< Task, SchedulingPolicy >:
Inheritance graph
Collaboration diagram for Nice::DispatcherThreadPool< Task, SchedulingPolicy >:
Collaboration graph

Classes

class  Worker
 

Public Member Functions

 DispatcherThreadPool (int _threads=3)
 
void init ()
 
 ~DispatcherThreadPool ()
 
void executeTask ()
 
bool schedule (TaskType const &_task)
 
size_t active () const
 
size_t pending () const
 
void clear ()
 
bool empty () const
 
void wait ()
 
void terminate ()
 
 DispatcherThreadPool (int _threads=3)
 
void init ()
 
 ~DispatcherThreadPool ()
 
void executeTask ()
 
bool schedule (TaskType const &_task)
 
size_t active () const
 
size_t pending () const
 
void clear ()
 
bool empty () const
 
void wait ()
 
void terminate ()
 

Protected Member Functions

void createThread ()
 
void createThread ()
 

Private Types

typedef Task TaskType
 Indicates the task's type. More...
 
typedef SchedulingPolicy< TaskTypeSchedulerType
 Indicates the scheduler's type. More...
 
typedef DispatcherThreadPool< Task, SchedulingPolicy > PoolType
 Indicates the thread pool's type. More...
 
typedef std::deque< Nice::ThreadPtrDispatcherThreadPoolContainer
 
typedef DispatcherThreadPoolContainer::iterator DispatcherThreadPoolContainerIterator
 
typedef DispatcherThreadPoolContainer::reverse_iterator DispatcherThreadPoolContainerReverseIterator
 
typedef Task TaskType
 Indicates the task's type. More...
 
typedef SchedulingPolicy< TaskTypeSchedulerType
 Indicates the scheduler's type. More...
 
typedef DispatcherThreadPool< Task, SchedulingPolicy > PoolType
 Indicates the thread pool's type. More...
 
typedef std::deque< Nice::ThreadPtrDispatcherThreadPoolContainer
 
typedef DispatcherThreadPoolContainer::iterator DispatcherThreadPoolContainerIterator
 
typedef DispatcherThreadPoolContainer::reverse_iterator DispatcherThreadPoolContainerReverseIterator
 

Private Member Functions

void start ()
 
void start ()
 

Private Attributes

size_t m_maxThreads
 
Nice::Atomic< size_t > m_availThreads
 
Nice::Atomic< size_t > m_activeThreads
 
SchedulerType m_scheduler
 
DispatcherThreadPoolContainer m_workers
 
IceUtil::Monitor< Nice::Mutexm_monitor
 
Nice::Atomic< boolm_terminated
 

Detailed Description

template<typename Task, template< typename > class SchedulingPolicy>
class Nice::DispatcherThreadPool< Task, SchedulingPolicy >

Thread pool.

Thread pools are a mechanism for asynchronous and parallel processing within the same process. The pool class provides a convenient way for dispatching asynchronous tasks as functions objects. The scheduling of these tasks can be easily controlled by using customized schedulers. A task must not throw an exception.

Parameters
TaskA function object which implements the operator 'void operator() (void) const'. The operator () is called by the pool to execute the task. Exceptions are ignored.
SchedulingPolicyA task container which determines how tasks are scheduled. It is guaranteed that this container is accessed only by one thread at a time. The scheduler shall not throw exceptions.
Remarks
The pool class is thread-safe.
See also
Tasks: DispatcherFunc, DispatcherFuncPrio
Scheduling policies: fifo_scheduler, DispatcherFuncPrio, DispatcherSchedulerPrio

Member Typedef Documentation

◆ DispatcherThreadPoolContainer [1/2]

template<typename Task , template< typename > class SchedulingPolicy>
typedef std::deque< Nice::ThreadPtr > Nice::DispatcherThreadPool< Task, SchedulingPolicy >::DispatcherThreadPoolContainer
private

◆ DispatcherThreadPoolContainer [2/2]

template<typename Task , template< typename > class SchedulingPolicy>
typedef std::deque< Nice::ThreadPtr > Nice::DispatcherThreadPool< Task, SchedulingPolicy >::DispatcherThreadPoolContainer
private

◆ DispatcherThreadPoolContainerIterator [1/2]

template<typename Task , template< typename > class SchedulingPolicy>
typedef DispatcherThreadPoolContainer::iterator Nice::DispatcherThreadPool< Task, SchedulingPolicy >::DispatcherThreadPoolContainerIterator
private

◆ DispatcherThreadPoolContainerIterator [2/2]

template<typename Task , template< typename > class SchedulingPolicy>
typedef DispatcherThreadPoolContainer::iterator Nice::DispatcherThreadPool< Task, SchedulingPolicy >::DispatcherThreadPoolContainerIterator
private

◆ DispatcherThreadPoolContainerReverseIterator [1/2]

template<typename Task , template< typename > class SchedulingPolicy>
typedef DispatcherThreadPoolContainer::reverse_iterator Nice::DispatcherThreadPool< Task, SchedulingPolicy >::DispatcherThreadPoolContainerReverseIterator
private

◆ DispatcherThreadPoolContainerReverseIterator [2/2]

template<typename Task , template< typename > class SchedulingPolicy>
typedef DispatcherThreadPoolContainer::reverse_iterator Nice::DispatcherThreadPool< Task, SchedulingPolicy >::DispatcherThreadPoolContainerReverseIterator
private

◆ PoolType [1/2]

template<typename Task , template< typename > class SchedulingPolicy>
typedef DispatcherThreadPool<Task, SchedulingPolicy> Nice::DispatcherThreadPool< Task, SchedulingPolicy >::PoolType
private

Indicates the thread pool's type.

◆ PoolType [2/2]

template<typename Task , template< typename > class SchedulingPolicy>
typedef DispatcherThreadPool<Task, SchedulingPolicy> Nice::DispatcherThreadPool< Task, SchedulingPolicy >::PoolType
private

Indicates the thread pool's type.

◆ SchedulerType [1/2]

template<typename Task , template< typename > class SchedulingPolicy>
typedef SchedulingPolicy<TaskType> Nice::DispatcherThreadPool< Task, SchedulingPolicy >::SchedulerType
private

Indicates the scheduler's type.

◆ SchedulerType [2/2]

template<typename Task , template< typename > class SchedulingPolicy>
typedef SchedulingPolicy<TaskType> Nice::DispatcherThreadPool< Task, SchedulingPolicy >::SchedulerType
private

Indicates the scheduler's type.

◆ TaskType [1/2]

template<typename Task , template< typename > class SchedulingPolicy>
typedef Task Nice::DispatcherThreadPool< Task, SchedulingPolicy >::TaskType
private

Indicates the task's type.

◆ TaskType [2/2]

template<typename Task , template< typename > class SchedulingPolicy>
typedef Task Nice::DispatcherThreadPool< Task, SchedulingPolicy >::TaskType
private

Indicates the task's type.

Constructor & Destructor Documentation

◆ DispatcherThreadPool() [1/2]

template<typename Task , template< typename > class SchedulingPolicy>
Nice::DispatcherThreadPool< Task, SchedulingPolicy >::DispatcherThreadPool ( int  _threads = 3)
inline

◆ ~DispatcherThreadPool() [1/2]

template<typename Task , template< typename > class SchedulingPolicy>
Nice::DispatcherThreadPool< Task, SchedulingPolicy >::~DispatcherThreadPool ( )
inline

◆ DispatcherThreadPool() [2/2]

template<typename Task , template< typename > class SchedulingPolicy>
Nice::DispatcherThreadPool< Task, SchedulingPolicy >::DispatcherThreadPool ( int  _threads = 3)
inline

◆ ~DispatcherThreadPool() [2/2]

template<typename Task , template< typename > class SchedulingPolicy>
Nice::DispatcherThreadPool< Task, SchedulingPolicy >::~DispatcherThreadPool ( )
inline

Member Function Documentation

◆ active() [1/2]

template<typename Task , template< typename > class SchedulingPolicy>
size_t Nice::DispatcherThreadPool< Task, SchedulingPolicy >::active ( ) const
inline

Returns the number of tasks which are currently executed.

Returns
The number of active tasks.

◆ active() [2/2]

template<typename Task , template< typename > class SchedulingPolicy>
size_t Nice::DispatcherThreadPool< Task, SchedulingPolicy >::active ( ) const
inline

Returns the number of tasks which are currently executed.

Returns
The number of active tasks.

◆ clear() [1/2]

template<typename Task , template< typename > class SchedulingPolicy>
void Nice::DispatcherThreadPool< Task, SchedulingPolicy >::clear ( )
inline

Removes all pending tasks from the pool's scheduler.

◆ clear() [2/2]

template<typename Task , template< typename > class SchedulingPolicy>
void Nice::DispatcherThreadPool< Task, SchedulingPolicy >::clear ( void  )
inline

Removes all pending tasks from the pool's scheduler.

◆ createThread() [1/2]

template<typename Task , template< typename > class SchedulingPolicy>
void Nice::DispatcherThreadPool< Task, SchedulingPolicy >::createThread ( )
inlineprotected

◆ createThread() [2/2]

template<typename Task , template< typename > class SchedulingPolicy>
void Nice::DispatcherThreadPool< Task, SchedulingPolicy >::createThread ( )
inlineprotected

◆ empty() [1/2]

template<typename Task , template< typename > class SchedulingPolicy>
bool Nice::DispatcherThreadPool< Task, SchedulingPolicy >::empty ( ) const
inline

Indicates that there are no tasks pending.

Returns
true if there are no tasks ready for execution.
Remarks
This function is more efficient that the check 'pending() == 0'.

◆ empty() [2/2]

template<typename Task , template< typename > class SchedulingPolicy>
bool Nice::DispatcherThreadPool< Task, SchedulingPolicy >::empty ( void  ) const
inline

Indicates that there are no tasks pending.

Returns
true if there are no tasks ready for execution.
Remarks
This function is more efficient that the check 'pending() == 0'.

◆ executeTask() [1/2]

template<typename Task , template< typename > class SchedulingPolicy>
void Nice::DispatcherThreadPool< Task, SchedulingPolicy >::executeTask ( )
inline

Executes a task from the scheduler. The task will be executed once only.

◆ executeTask() [2/2]

template<typename Task , template< typename > class SchedulingPolicy>
void Nice::DispatcherThreadPool< Task, SchedulingPolicy >::executeTask ( )
inline

Executes a task from the scheduler. The task will be executed once only.

◆ init() [1/2]

template<typename Task , template< typename > class SchedulingPolicy>
void Nice::DispatcherThreadPool< Task, SchedulingPolicy >::init ( )
inline

◆ init() [2/2]

template<typename Task , template< typename > class SchedulingPolicy>
void Nice::DispatcherThreadPool< Task, SchedulingPolicy >::init ( )
inline

◆ pending() [1/2]

template<typename Task , template< typename > class SchedulingPolicy>
size_t Nice::DispatcherThreadPool< Task, SchedulingPolicy >::pending ( ) const
inline

Returns the number of tasks which are ready for execution.

Returns
The number of pending tasks.

◆ pending() [2/2]

template<typename Task , template< typename > class SchedulingPolicy>
size_t Nice::DispatcherThreadPool< Task, SchedulingPolicy >::pending ( ) const
inline

Returns the number of tasks which are ready for execution.

Returns
The number of pending tasks.

◆ schedule() [1/2]

template<typename Task , template< typename > class SchedulingPolicy>
bool Nice::DispatcherThreadPool< Task, SchedulingPolicy >::schedule ( TaskType const _task)
inline

Schedules a task for asynchronous execution.

Parameters
taskThe task function object. It should not throw execeptions.
Returns
true, if the task could be scheduled and false otherwise.

◆ schedule() [2/2]

template<typename Task , template< typename > class SchedulingPolicy>
bool Nice::DispatcherThreadPool< Task, SchedulingPolicy >::schedule ( TaskType const _task)
inline

Schedules a task for asynchronous execution.

Parameters
taskThe task function object. It should not throw execeptions.
Returns
true, if the task could be scheduled and false otherwise.

◆ start() [1/2]

template<typename Task , template< typename > class SchedulingPolicy>
void Nice::DispatcherThreadPool< Task, SchedulingPolicy >::start ( )
private

◆ start() [2/2]

template<typename Task , template< typename > class SchedulingPolicy>
void Nice::DispatcherThreadPool< Task, SchedulingPolicy >::start ( )
private

◆ terminate() [1/2]

template<typename Task , template< typename > class SchedulingPolicy>
void Nice::DispatcherThreadPool< Task, SchedulingPolicy >::terminate ( )
inline

◆ terminate() [2/2]

template<typename Task , template< typename > class SchedulingPolicy>
void Nice::DispatcherThreadPool< Task, SchedulingPolicy >::terminate ( )
inline

◆ wait() [1/2]

template<typename Task , template< typename > class SchedulingPolicy>
void Nice::DispatcherThreadPool< Task, SchedulingPolicy >::wait ( )
inline

◆ wait() [2/2]

template<typename Task , template< typename > class SchedulingPolicy>
void Nice::DispatcherThreadPool< Task, SchedulingPolicy >::wait ( )
inline

Member Data Documentation

◆ m_activeThreads

template<typename Task , template< typename > class SchedulingPolicy>
Nice::Atomic< size_t > Nice::DispatcherThreadPool< Task, SchedulingPolicy >::m_activeThreads
private

◆ m_availThreads

template<typename Task , template< typename > class SchedulingPolicy>
Nice::Atomic< size_t > Nice::DispatcherThreadPool< Task, SchedulingPolicy >::m_availThreads
private

◆ m_maxThreads

template<typename Task , template< typename > class SchedulingPolicy>
size_t Nice::DispatcherThreadPool< Task, SchedulingPolicy >::m_maxThreads
private

◆ m_monitor

template<typename Task , template< typename > class SchedulingPolicy>
IceUtil::Monitor< Nice::Mutex > Nice::DispatcherThreadPool< Task, SchedulingPolicy >::m_monitor
private

◆ m_scheduler

template<typename Task , template< typename > class SchedulingPolicy>
SchedulerType Nice::DispatcherThreadPool< Task, SchedulingPolicy >::m_scheduler
private

◆ m_terminated

template<typename Task , template< typename > class SchedulingPolicy>
Nice::Atomic< bool > Nice::DispatcherThreadPool< Task, SchedulingPolicy >::m_terminated
private

◆ m_workers

template<typename Task , template< typename > class SchedulingPolicy>
DispatcherThreadPoolContainer Nice::DispatcherThreadPool< Task, SchedulingPolicy >::m_workers
private

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