RR Thread Pool
 All Classes Functions Typedefs Groups Pages
Public Member Functions | Static Public Member Functions | List of all members
IThreadPool Class Referenceabstract

General purpose thread pool for inter-thread communication. More...

#include <ThreadPool.h>

Public Member Functions

virtual ~IThreadPool ()
 Destructor.
 
virtual std::size_t push (Task task)=0
 Pushes one task into the pool. More...
 
virtual std::size_t pop (Task &task, bool blocking)=0
 Pops one executed/cancelled task from the pool. More...
 
virtual void cancel ()=0
 Cancel the pool functionality indefinitely releasing any thread. More...
 
virtual void join ()=0
 Cancel and wait for the termination of pool's threads. More...
 
template<typename Derived >
std::size_t popT (std::shared_ptr< Derived > &task, bool blocking)
 Convenient template method to pop executed tasks. More...
 

Static Public Member Functions

static IThreadPoolcreate (std::size_t num_threads, std::size_t task_capacity=std::numeric_limits< std::size_t >::max())
 Factory method to create a thread pool implemented for the current platform. More...
 

Detailed Description

General purpose thread pool for inter-thread communication.

The class is defined as a pure abstract class with a factory method (see IMessageQueue::create) to build platform-specific implementations of the queue while maintaining a platform-agnostic interface.

The class is 100% thread safe.

Member Function Documentation

virtual void IThreadPool::cancel ( )
pure virtual

Cancel the pool functionality indefinitely releasing any thread.

Also cancel any task that have not yet executed. Those task are queued on the list of executed one and can be popped (see method pop).

The cancelled status is not reversible and is meant mainly as an action to be performed before the pool destruction.

Warning
Doesn't wait for the peer threads to be released, just broadcast a signal to them.
static IThreadPool* IThreadPool::create ( std::size_t  num_threads,
std::size_t  task_capacity = std::numeric_limits< std::size_t >::max() 
)
static

Factory method to create a thread pool implemented for the current platform.

Parameters
num_threadsThe number of threads the pool should use concurrently.
task_capacityMaximum number of tasks that can be queued at the same time before their execution. By default this limit is relaxed as much as possible.
Returns
The newly created thread pool.
virtual void IThreadPool::join ( )
pure virtual

Cancel and wait for the termination of pool's threads.

This method calls the method cancel and than waits indefinitely for the pool's threads.

Precondition
  • The behaviour of this method if called during the task execution is undefined.
virtual std::size_t IThreadPool::pop ( Task &  task,
bool  blocking 
)
pure virtual

Pops one executed/cancelled task from the pool.

Parameters
[out]taskSmart pointer that will be reset with the popped task in case of success.
blockingIf set to true the method blocks the current thread indefinitely until a new task have been executed or the pool have been cancelled.
Returns
  • On success, the number of tasks already completed not yet popped before the extraction, that is at least one.
  • On failure, zero (parameter task is not touched in that case).
Precondition
  • The pool have not been cancelled.
template<typename Derived >
std::size_t IThreadPool::popT ( std::shared_ptr< Derived > &  task,
bool  blocking 
)
inline

Convenient template method to pop executed tasks.

Since by design the user should derive messages from the class ITask this template take care to dynamic cast shared pointers to the user defined derived class.

This method is meant to be used when the user uses the pool with one single derived class and hence don't need to manually dynamic cast every popped message from the generic ITask.

virtual std::size_t IThreadPool::push ( Task  task)
pure virtual

Pushes one task into the pool.

Parameters
taskThe task to be inserted.
Returns
  • On success, the number of tasks pending to be executed after the insertion, that is at least one.
  • On failure, zero. This may happen if the maximum allowed capacity for pending tasks have been reached.
Precondition
  • The parameter task is not null.
  • The pool have not been cancelled.

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