![]() |
Implementation of a thread pool. More...
#include <ThreadPool.h>
Inherits blaze::NonCopyable.
Public Member Functions | |
Constructor | |
ThreadPool (size_t n) | |
Constructor for the ThreadPool class. More... | |
Destructor | |
~ThreadPool () | |
Destructor for the ThreadPool class. More... | |
Get functions | |
bool | isEmpty () const |
Returns whether any tasks are scheduled for execution. More... | |
size_t | size () const |
Returns the current size of the thread pool. More... | |
size_t | active () const |
Returns the number of currently active/busy threads. More... | |
size_t | ready () const |
Returns the number of currently ready/inactive threads. More... | |
Scheduling functions | |
template<typename Callable > | |
void | schedule (Callable func) |
Scheduling the given zero argument function/functor for execution. More... | |
template<typename Callable , typename A1 > | |
void | schedule (Callable func, A1 a1) |
Scheduling the given unary function/functor for execution. More... | |
template<typename Callable , typename A1 , typename A2 > | |
void | schedule (Callable func, A1 a1, A2 a2) |
Scheduling the given binary function/functor for execution. More... | |
template<typename Callable , typename A1 , typename A2 , typename A3 > | |
void | schedule (Callable func, A1 a1, A2 a2, A3 a3) |
Scheduling the given ternary function/functor for execution. More... | |
template<typename Callable , typename A1 , typename A2 , typename A3 , typename A4 > | |
void | schedule (Callable func, A1 a1, A2 a2, A3 a3, A4 a4) |
Scheduling the given four argument function/functor for execution. More... | |
template<typename Callable , typename A1 , typename A2 , typename A3 , typename A4 , typename A5 > | |
void | schedule (Callable func, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5) |
Scheduling the given four argument function/functor for execution. More... | |
Utility functions | |
void | resize (size_t n) |
Changes the total number of threads in the thread pool. More... | |
void | wait () |
Waiting for all scheduled tasks to be completed. More... | |
void | clear () |
Removing all scheduled tasks from the thread pool. More... | |
Private Types | |
typedef PtrVector< Thread > | Threads |
Type of the thread container. | |
typedef threadpool::TaskQueue | TaskQueue |
Type of the task queue. | |
typedef boost::mutex | Mutex |
Type of the mutex. | |
typedef Mutex::scoped_lock | Lock |
Type of a locking object. | |
typedef boost::condition_variable | Condition |
Condition variable type. | |
Private Member Functions | |
Thread functions | |
void | createThread () |
Adding a new thread to the thread pool. More... | |
bool | executeTask () |
Executing a scheduled task. More... | |
Private Attributes | |
Member variables | |
volatile size_t | total_ |
Total number of threads in the thread pool. | |
volatile size_t | expected_ |
Expected number of threads in the thread pool. More... | |
volatile size_t | active_ |
Number of currently active/busy threads. | |
Threads | threads_ |
The threads contained in the thread pool. | |
TaskQueue | taskqueue_ |
Task queue for the scheduled tasks. | |
Mutex | mutex_ |
Synchronization mutex. | |
Condition | waitForTask_ |
Wait condition for idle threads. | |
Condition | waitForThread_ |
Wait condition for the thread management. | |
Implementation of a thread pool.
The ThreadPool class represents a thread pool according to the thread pool pattern (see for example http://en.wikipedia.org/wiki/Thread_pool_pattern). It manages a certain number of threads in order to process a larger number of independent tasks.
The primary purpose of a thread pool is the reuse of system resources: instead of creating a single thread for every individual task, threads are reused to handle several tasks. This increases the performance in comparison to different threading strategies, as illustrated in the graph below. The first bar indicates the sequential performance of 1000 matrix-matrix multiplications of arbitrarily sized square matrices. The second bar shows the performance of the same work performed by 1000 distinct threads (i.e. one thread for each matrix-matrix multiplication) on a quad-core system. In this case, all cores of the system can be used, but the additional overhead of creating and managing new threads prevents the expected performance increase by a factor of four. The third bar illustrates the performance of four threads distributing the work between them (i.e. 250 matrix-matrix multiplications per thread), again using the same quad-core system. This approach nearly achieves four times the performance of the sequential execution. The fourth bar represents the performance of the ThreadPool class using fourth threads for the execution of the 1000 individual multiplications.
Additionally, the thread pool approach simplifies load balancing and increases the stability of the system.
The following example demonstrates the use of the ThreadPool class. In contrast to the setup of individual threads (see the Thread class description for more details), it is not necessary to create and manage individual threads, but only to schedules tasks for the accordingly sized thread pool.
Note that the ThreadPool class schedule() function allows for up to five arguments for the given functions/functors.
It can happen that during the execution of a given task a thread encounters an erroneous situation and has to throw an exception. However, exceptions thrown in the usual way cannot be caught by a try-catch-block in the main thread of execution:
The only possible way to transport exceptions between threads is to use the according boost functionality demonstrated in the following example. Note that any function/functor scheduled for execution is responsible to handle exceptions in this way!
|
explicit |
Constructor for the ThreadPool class.
n | Initial number of threads ![]() |
This constructor creates a thread pool with initially n new threads. All threads are initially idle until a task is scheduled.
blaze::ThreadPool::~ThreadPool | ( | ) |
Destructor for the ThreadPool class.
The destructor clears all remaining tasks from the task queue and waits for the currently active threads to complete their tasks.
|
inline |
Returns the number of currently active/busy threads.
void blaze::ThreadPool::clear | ( | ) |
Removing all scheduled tasks from the thread pool.
This function removes all currently scheduled tasks from the thread pool. The total number of threads remains unchanged and all active threads continue completing their tasks.
|
private |
Adding a new thread to the thread pool.
|
private |
Executing a scheduled task.
This function is repeatedly called by every thread to execute one of the scheduled tasks. In case there is no task available, the thread blocks and waits for a new task to be scheduled.
|
inline |
Returns whether any tasks are scheduled for execution.
|
inline |
Returns the number of currently ready/inactive threads.
void blaze::ThreadPool::resize | ( | size_t | n | ) |
Changes the total number of threads in the thread pool.
n | The new number of threads ![]() |
std::invalid_argument | Invalid number of threads. |
This function changes the size of the thread pool, i.e. changes the total number of threads contained in the pool. If n is smaller than the current size of the thread pool, the according number of threads is removed from the pool, otherwise new threads are added to the pool.
void blaze::ThreadPool::schedule | ( | Callable | func | ) |
Scheduling the given zero argument function/functor for execution.
func | The given function/functor. |
This function schedules the given function/functor for execution. The given function/functor must be copyable, must be callable without arguments and must return void.
void blaze::ThreadPool::schedule | ( | Callable | func, |
A1 | a1 | ||
) |
Scheduling the given unary function/functor for execution.
func | The given function/functor. |
a1 | The first argument. |
This function schedules the given function/functor for execution. The given function/functor must be copyable, must be callable with one argument and must return void.
void blaze::ThreadPool::schedule | ( | Callable | func, |
A1 | a1, | ||
A2 | a2 | ||
) |
Scheduling the given binary function/functor for execution.
func | The given function/functor. |
a1 | The first argument. |
a2 | The second argument. |
This function schedules the given function/functor for execution. The given function/functor must be copyable, must be callable with two arguments and must return void.
void blaze::ThreadPool::schedule | ( | Callable | func, |
A1 | a1, | ||
A2 | a2, | ||
A3 | a3 | ||
) |
Scheduling the given ternary function/functor for execution.
func | The given function/functor. |
a1 | The first argument. |
a2 | The second argument. |
a3 | The third argument. |
This function schedules the given function/functor for execution. The given function/functor must be copyable, must be callable with three arguments and must return void.
void blaze::ThreadPool::schedule | ( | Callable | func, |
A1 | a1, | ||
A2 | a2, | ||
A3 | a3, | ||
A4 | a4 | ||
) |
Scheduling the given four argument function/functor for execution.
func | The given function/functor. |
a1 | The first argument. |
a2 | The second argument. |
a3 | The third argument. |
a4 | The fourth argument. |
This function schedules the given function/functor for execution. The given function/functor must be copyable, must be callable with four arguments and must return void.
void blaze::ThreadPool::schedule | ( | Callable | func, |
A1 | a1, | ||
A2 | a2, | ||
A3 | a3, | ||
A4 | a4, | ||
A5 | a5 | ||
) |
Scheduling the given four argument function/functor for execution.
func | The given function/functor. |
a1 | The first argument. |
a2 | The second argument. |
a3 | The third argument. |
a4 | The fourth argument. |
a5 | The fifth argument. |
This function schedules the given function/functor for execution. The given function/functor must be copyable, must be callable with five arguments and must return void.
|
inline |
Returns the current size of the thread pool.
void blaze::ThreadPool::wait | ( | ) |
Waiting for all scheduled tasks to be completed.
This function blocks until all scheduled tasks have been completed.
|
private |
Expected number of threads in the thread pool.
This number may differ from the total number of threads during a resize of the thread pool.