Creates threads that computes a function in a multithreaded fashion. More...
#include <WThreadedFunction.h>
Public Types | |
typedef boost::function< void(WException const &) > | ExceptionFunction |
a type for exception callbacks | |
Public Member Functions | |
WThreadedFunction (std::size_t numThreads, boost::shared_ptr< Function_T > function) | |
Creates the thread pool with a given number of threads. | |
virtual | ~WThreadedFunction () |
Destroys the thread pool and stops all threads, if any one of them is still running. | |
virtual void | run () |
Starts the threads. | |
virtual void | stop () |
Request all threads to stop. | |
virtual void | wait () |
Wait for all threads to stop. | |
Private Types | |
typedef boost::signal< void(WException const &) > | ExceptionSignal |
a type for exception signals | |
Private Member Functions | |
WThreadedFunction (WThreadedFunction const &) | |
WThreadedFunction is non-copyable, so the copy constructor is not implemented. | |
WThreadedFunction & | operator= (WThreadedFunction const &) |
WThreadedFunction is non-copyable, so the copy operator is not implemented. | |
void | handleThreadDone () |
This function gets subscribed to the threads' stop signals. | |
void | handleThreadException (WException const &e) |
This function handles exceptions thrown in the worker threads. | |
Private Attributes | |
std::size_t | m_numThreads |
the number of threads to manage | |
std::vector< boost::shared_ptr < WWorkerThread< Function_T > > > | m_threads |
the threads | |
boost::shared_ptr< Function_T > | m_func |
the function object | |
WSharedObject< std::size_t > | m_threadsDone |
a counter that keeps track of how many threads have finished |
Creates threads that computes a function in a multithreaded fashion.
The template parameter is an object that provides a function to execute. The following function needs to be implemented:
void operator ( std::size_t id, std::size_t mx, WBoolFlag const& s );
Here, 'id' is the number of the thread currently executing the function, ranging from 0 to mx - 1, where 'mx' is the number of threads running. 's' is a flag that indicates if the execution should be stopped. Make sure to check the flag often, so that the threads can be stopped when needed.
This class itself is NOT thread-safe, do not access it from different threads simultaneously. Also, make sure any resources used by your function are accessed in a threadsafe manner, as all threads share the same function object.
Any exception thrown by your function will be caught and forwarded via the exception signal. Beware that the signal function will be called in the executing threads, as opposed to in your module thread. This means that the exception handler bound to the exception signal must be threadsafe too.
The status of the execution can be checked via the status() function. Also, when all threads finish (due to throwing exceptions or actually successfully finishing computation ), a condition will be notified.
Definition at line 176 of file WThreadedFunction.h.
typedef boost::function< void ( WException const& ) > WThreadedFunction< Function_T >::ExceptionFunction |
a type for exception callbacks
Reimplemented from WThreadedFunctionBase.
Definition at line 184 of file WThreadedFunction.h.
typedef boost::signal< void ( WException const& ) > WThreadedFunction< Function_T >::ExceptionSignal [private] |
a type for exception signals
Reimplemented from WThreadedFunctionBase.
Definition at line 179 of file WThreadedFunction.h.
WThreadedFunction< Function_T >::WThreadedFunction | ( | std::size_t | numThreads, | |
boost::shared_ptr< Function_T > | function | |||
) |
Creates the thread pool with a given number of threads.
numThreads | The number of threads to create. | |
function | The function object. |
Definition at line 259 of file WThreadedFunction.h.
References WSharedObject< T >::getWriteTicket(), WThreadedFunction< Function_T >::handleThreadDone(), WThreadedFunction< Function_T >::handleThreadException(), WThreadedFunction< Function_T >::m_func, WThreadedFunction< Function_T >::m_numThreads, WThreadedFunction< Function_T >::m_threads, and WThreadedFunction< Function_T >::m_threadsDone.
WThreadedFunction< Function_T >::~WThreadedFunction | ( | ) | [virtual] |
Destroys the thread pool and stops all threads, if any one of them is still running.
Definition at line 295 of file WThreadedFunction.h.
References WThreadedFunction< Function_T >::stop().
WThreadedFunction< Function_T >::WThreadedFunction | ( | WThreadedFunction< Function_T > const & | ) | [private] |
WThreadedFunction is non-copyable, so the copy constructor is not implemented.
void WThreadedFunction< Function_T >::handleThreadDone | ( | ) | [private] |
This function gets subscribed to the threads' stop signals.
Definition at line 340 of file WThreadedFunction.h.
References WSharedObject< T >::getWriteTicket(), WThreadedFunctionBase::m_doneCondition, WThreadedFunction< Function_T >::m_numThreads, WThreadedFunctionBase::m_status, and WThreadedFunction< Function_T >::m_threadsDone.
Referenced by WThreadedFunction< Function_T >::handleThreadException(), and WThreadedFunction< Function_T >::WThreadedFunction().
void WThreadedFunction< Function_T >::handleThreadException | ( | WException const & | e | ) | [private] |
This function handles exceptions thrown in the worker threads.
e | The exception that was thrown. |
Definition at line 371 of file WThreadedFunction.h.
References WSharedObject< T >::getWriteTicket(), WThreadedFunction< Function_T >::handleThreadDone(), WThreadedFunctionBase::m_exceptionSignal, and WThreadedFunctionBase::m_status.
Referenced by WThreadedFunction< Function_T >::WThreadedFunction().
WThreadedFunction& WThreadedFunction< Function_T >::operator= | ( | WThreadedFunction< Function_T > const & | ) | [private] |
WThreadedFunction is non-copyable, so the copy operator is not implemented.
void WThreadedFunction< Function_T >::run | ( | ) | [virtual] |
Starts the threads.
Implements WThreadedFunctionBase.
Definition at line 301 of file WThreadedFunction.h.
References WSharedObject< T >::getWriteTicket(), WThreadedFunction< Function_T >::m_numThreads, WThreadedFunctionBase::m_status, WThreadedFunction< Function_T >::m_threads, and WThreadedFunction< Function_T >::m_threadsDone.
Referenced by WThreadedFunctionTest::testExceptionHandling(), WThreadedFunctionTest::testMultipleThreads(), WThreadedPerVoxelOperationTest::testMultithreadedFunction(), WThreadedFunctionTest::testStopCondition(), and WThreadedFunctionTest::testStopThreads().
void WThreadedFunction< Function_T >::stop | ( | ) | [virtual] |
Request all threads to stop.
Returns immediately, so you might have to wait() for the threads to actually finish.
Implements WThreadedFunctionBase.
Definition at line 315 of file WThreadedFunction.h.
References WSharedObject< T >::getWriteTicket(), WThreadedFunctionBase::m_status, and WThreadedFunction< Function_T >::m_threads.
Referenced by WThreadedFunctionTest::testStopThreads(), and WThreadedFunction< Function_T >::~WThreadedFunction().
void WThreadedFunction< Function_T >::wait | ( | ) | [virtual] |
Wait for all threads to stop.
Implements WThreadedFunctionBase.
Definition at line 329 of file WThreadedFunction.h.
References WThreadedFunction< Function_T >::m_threads.
Referenced by WThreadedFunctionTest::testExceptionHandling(), WThreadedFunctionTest::testMultipleThreads(), WThreadedPerVoxelOperationTest::testMultithreadedFunction(), and WThreadedFunctionTest::testStopThreads().
boost::shared_ptr< Function_T > WThreadedFunction< Function_T >::m_func [private] |
the function object
Definition at line 252 of file WThreadedFunction.h.
Referenced by WThreadedFunction< Function_T >::WThreadedFunction().
std::size_t WThreadedFunction< Function_T >::m_numThreads [private] |
the number of threads to manage
Definition at line 245 of file WThreadedFunction.h.
Referenced by WThreadedFunction< Function_T >::handleThreadDone(), WThreadedFunction< Function_T >::run(), and WThreadedFunction< Function_T >::WThreadedFunction().
std::vector< boost::shared_ptr< WWorkerThread< Function_T > > > WThreadedFunction< Function_T >::m_threads [private] |
the threads
Definition at line 249 of file WThreadedFunction.h.
Referenced by WThreadedFunction< Function_T >::run(), WThreadedFunction< Function_T >::stop(), WThreadedFunction< Function_T >::wait(), and WThreadedFunction< Function_T >::WThreadedFunction().
WSharedObject< std::size_t > WThreadedFunction< Function_T >::m_threadsDone [private] |
a counter that keeps track of how many threads have finished
Definition at line 255 of file WThreadedFunction.h.
Referenced by WThreadedFunction< Function_T >::handleThreadDone(), WThreadedFunction< Function_T >::run(), and WThreadedFunction< Function_T >::WThreadedFunction().