![]() |
Implementation of a single thread of execution. More...
#include <Thread.h>
Inherits blaze::NonCopyable.
Public Member Functions | |
Destructor | |
~Thread () | |
Destructor for the Thread class. | |
Private Types | |
typedef TT | ThreadType |
Type of the encapsulated thread. | |
typedef ThreadPool< TT, MT, LT, CT > | ThreadPoolType |
Type of the managing thread pool. | |
typedef boost::scoped_ptr< ThreadType > | ThreadHandle |
Handle for a single thread. | |
Private Member Functions | |
Thread execution functions | |
void | run () |
Execution function for threads in a thread pool. More... | |
Private Attributes | |
Member variables | |
volatile bool | terminated_ |
Thread termination flag. More... | |
ThreadPoolType * | pool_ |
Handle to the managing thread pool. | |
ThreadHandle | thread_ |
Handle to the thread of execution. | |
Constructors | |
Thread (ThreadPoolType *pool) | |
Starting a thread in a thread pool. More... | |
template<typename Callable > | |
Thread (Callable func) | |
Starting a thread of execution on the given zero argument function/functor. More... | |
template<typename Callable , typename A1 > | |
Thread (Callable func, A1 a1) | |
Starting a thread of execution on the given unary function/functor. More... | |
template<typename Callable , typename A1 , typename A2 > | |
Thread (Callable func, A1 a1, A2 a2) | |
Starting a thread of execution on the given binary function/functor. More... | |
template<typename Callable , typename A1 , typename A2 , typename A3 > | |
Thread (Callable func, A1 a1, A2 a2, A3 a3) | |
Starting a thread of execution on the given ternary function/functor. More... | |
template<typename Callable , typename A1 , typename A2 , typename A3 , typename A4 > | |
Thread (Callable func, A1 a1, A2 a2, A3 a3, A4 a4) | |
Starting a thread of execution on the given four argument function/functor. More... | |
template<typename Callable , typename A1 , typename A2 , typename A3 , typename A4 , typename A5 > | |
Thread (Callable func, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5) | |
Starting a thread of execution on the given five argument function/functor. More... | |
Utility functions | |
bool | hasTerminated () const |
Returns whether the thread has terminated its execution. More... | |
bool | joinable () const |
Returns whether this is a thread of execution. More... | |
void | join () |
Waiting for a thread of execution to complete. More... | |
Implementation of a single thread of execution.
The Thread template represents a thread of execution for the parallel execution of concurrent tasks. Each Thread object incorporates a single thread of execution, or Not-a-Thread, and at most one Thread object incorporates a given thread of execution since it is not possible to copy a Thread.
The implementation of the Thread class template is based on the implementation of standard threads as provided by the C++11 standard or the Boost library. Via the four template parameters it is possible to configure a Thread instance as either a C++11 thread or as Boost thread:
std::thread
, boost::thread
, or any other standard conforming thread type.std::mutex
, boost::mutex
, or any other standard conforming mutex type.std::unique_lock
, boost::unique_lock
.std::condition_variable
, boost::condition_variable
, or any other standard conforming condition variable type.Examples:
For more information about the standard thread functionality, see [1] or [2] or the current documentation at the Boost homepage: www.boost.org.
The Blaze library provides the functionality to create individual threads for specific tasks, or to create thread pools for the execution of a larger number of tasks (see the ThreadPool class description). The following example demonstrates the setup of individual threads to handle specific tasks. In this example, a function without arguments and a functor with two arguments are executed in parallel by two distinct threads:
Note that the Thread class allows for up to five arguments for the given functions/functors. Also note that the two tasks are not executed in parallel since the join() function is used to wait for each thread's completion.
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:
For a detailed explanation how to portably transport exceptions between threads, see [1] or [2]. In case of the Boost library, the according Boost functionality as demonstrated in the following example has to be used. Note that any function/functor passed to a thread is responsible to handle exceptions in this way!
[1] A. Williams: C++ Concurrency in Action, Manning, 2012, ISBN: 978-1933988771
[2] B. Stroustrup: The C++ Programming Language, Addison-Wesley, 2013, ISBN: 978-0321563842
|
explicitprivate |
Starting a thread in a thread pool.
pool | Handle to the managing thread pool. |
This function creates a new thread in the given thread pool. The thread is kept alive until explicitly killed by the managing thread pool.
|
inlineexplicit |
Starting a thread of execution on the given zero argument function/functor.
func | The given function/functor. |
This function creates a new thread of execution on the given function/functor. The given function/functor must be copyable, must be callable without arguments and must return void.
|
inlineexplicit |
Starting a thread of execution on the given unary function/functor.
func | The given function/functor. |
a1 | The first argument. |
This function creates a new thread of execution on the given function/functor. The given function/functor must be copyable, must be callable with a single argument and must return void.
|
inlineexplicit |
Starting a thread of execution on the given binary function/functor.
func | The given function/functor. |
a1 | The first argument. |
a2 | The second argument. |
This function creates a new thread of execution on the given function/functor. The given function/functor must be copyable, must be callable with two arguments and must return void.
|
inlineexplicit |
Starting a thread of execution on the given ternary function/functor.
func | The given function/functor. |
a1 | The first argument. |
a2 | The second argument. |
a3 | The third argument. |
This function creates a new thread of execution on the given function/functor. The given function/functor must be copyable, must be callable with three arguments and must return void.
|
inlineexplicit |
Starting a thread of execution on the given four argument function/functor.
func | The given function/functor. |
a1 | The first argument. |
a2 | The second argument. |
a3 | The third argument. |
a4 | The fourth argument. |
This function creates a new thread of execution on the given function/functor. The given function/functor must be copyable, must be callable with four arguments and must return void.
|
inlineexplicit |
Starting a thread of execution on the given five argument function/functor.
func | The given function/functor. |
a1 | The first argument. |
a2 | The second argument. |
a3 | The third argument. |
a4 | The fourth argument. |
a5 | The firth argument. |
This function creates a new thread of execution on the given function/functor. The given function/functor must be copyable, must be callable with five arguments and must return void.
|
inlineprivate |
Returns whether the thread has terminated its execution.
This function is used by the managing thread pool to learn whether the thread has finished its execution and can be destroyed.
|
inline |
Waiting for a thread of execution to complete.
If the thread is still executing the given task, this function blocks until the thread's tasks is completed.
|
inline |
Returns whether this is a thread of execution.
This function returns whether this thread is still executing the given task or if it has already finished the job. In case the thread is still execution, the function returns true, else it returns false.
|
private |
Execution function for threads in a thread pool.
This function is executed by any thread managed by a thread pool.
|
private |
Thread termination flag.
This flag value is used by the managing thread pool to learn whether the thread has terminated its execution.