|
| Thread (ThreadPool *pool) |
| Starting a thread in a thread pool.
|
|
template<typename Callable > |
| Thread (Callable func) |
| Starting a thread of execution on the given zero argument function/functor.
|
|
template<typename Callable , typename A1 > |
| Thread (Callable func, A1 a1) |
| Starting a thread of execution on the given unary function/functor.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
Implementation of a single thread of execution.
General
The Thread class 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 is based on the implementation of the boost library. For more information about boost threads, see the current documentation at the boost homepage: www.boost.org.
Creating individual threads
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:
void function0() { ... }
struct Functor2
{
void operator()( int a, int b ) { ... }
};
int main()
{
thread1.join();
thread2.join();
}
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.
Throwing exceptions in a thread parallel environment
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:
void task()
{
...
throw std::runtime_error( ... );
...
}
try {
thread.join();
}
catch( ... )
{
...
}
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 passed to a thread is responsible to handle exceptions in this way!
#include <boost/bind.hpp>
#include <boost/exception_ptr.hpp>
void throwException()
{
...
throw boost::enable_current_exception( std::runtime_error( ... ) );
...
}
void task( boost::exception_ptr&
error )
{
try {
throwException();
error = boost::exception_ptr();
}
catch( ... ) {
error = boost::current_exception();
}
}
void work()
{
boost::exception_ptr
error;
Thread thread( boost::bind( task, boost::ref(error) ) );
thread.join();
if( error ) {
std::cerr << " Exception during thread execution!\n\n";
boost::rethrow_exception( error );
}
}