OpenWalnut
1.5.0dev
|
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 More... | |
Public Types inherited from WThreadedFunctionBase | |
typedef boost::function< void(WException const &) > | ExceptionFunction |
a type for exception callbacks More... | |
Public Member Functions | |
WThreadedFunction (std::size_t numThreads, std::shared_ptr< Function_T > function) | |
Creates the thread pool with a given number of threads. More... | |
virtual | ~WThreadedFunction () |
Destroys the thread pool and stops all threads, if any one of them is still running. More... | |
virtual void | run () |
Starts the threads. More... | |
virtual void | stop () |
Request all threads to stop. More... | |
virtual void | wait () |
Wait for all threads to stop. More... | |
Public Member Functions inherited from WThreadedFunctionBase | |
WThreadedFunctionBase () | |
Standard constructor. More... | |
virtual | ~WThreadedFunctionBase () |
Destroys the thread pool and stops all threads, if any one of them is still running. More... | |
WThreadedFunctionStatus | status () |
Get the status of the threads. More... | |
std::shared_ptr< WCondition > | getThreadsDoneCondition () |
Returns a condition that gets fired when all threads have finished. More... | |
void | subscribeExceptionSignal (ExceptionFunction func) |
Subscribe a function to an exception signal. More... | |
Private Types | |
typedef boost::signals2::signal< void(WException const &) > | ExceptionSignal |
a type for exception signals More... | |
Private Member Functions | |
WThreadedFunction (WThreadedFunction const &) | |
WThreadedFunction is non-copyable, so the copy constructor is not implemented. More... | |
WThreadedFunction & | operator= (WThreadedFunction const &) |
WThreadedFunction is non-copyable, so the copy operator is not implemented. More... | |
void | handleThreadDone () |
This function gets subscribed to the threads' stop signals. More... | |
void | handleThreadException (WException const &e) |
This function handles exceptions thrown in the worker threads. More... | |
Private Attributes | |
std::size_t | m_numThreads |
the number of threads to manage More... | |
std::vector< std::shared_ptr< WWorkerThread< Function_T > > > | m_threads |
the threads More... | |
std::shared_ptr< Function_T > | m_func |
the function object More... | |
WSharedObject< std::size_t > | m_threadsDone |
a counter that keeps track of how many threads have finished More... | |
Additional Inherited Members | |
Protected Member Functions inherited from WThreadedFunctionBase | |
WThreadedFunctionBase (WThreadedFunctionBase const &) | |
WThreadedFunctionBase is non-copyable, so the copy constructor is not implemented. More... | |
WThreadedFunctionBase & | operator= (WThreadedFunctionBase const &) |
WThreadedFunctionBase is non-copyable, so the copy operator is not implemented. More... | |
Protected Attributes inherited from WThreadedFunctionBase | |
std::shared_ptr< WCondition > | m_doneCondition |
a condition that gets notified when the work is complete More... | |
ExceptionSignal | m_exceptionSignal |
a signal for exceptions More... | |
WSharedObject< WThreadedFunctionStatus > | m_status |
the current status More... | |
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 175 of file WThreadedFunction.h.
typedef boost::function< void ( WException const& ) > WThreadedFunction< Function_T >::ExceptionFunction |
a type for exception callbacks
Definition at line 182 of file WThreadedFunction.h.
|
private |
a type for exception signals
Definition at line 178 of file WThreadedFunction.h.
WThreadedFunction< Function_T >::WThreadedFunction | ( | std::size_t | numThreads, |
std::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 257 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.
|
virtual |
Destroys the thread pool and stops all threads, if any one of them is still running.
Definition at line 293 of file WThreadedFunction.h.
|
private |
WThreadedFunction is non-copyable, so the copy constructor is not implemented.
|
private |
This function gets subscribed to the threads' stop signals.
Definition at line 338 of file WThreadedFunction.h.
References WSharedObject< T >::getWriteTicket().
Referenced by WThreadedFunction< Function_T >::WThreadedFunction().
|
private |
This function handles exceptions thrown in the worker threads.
e | The exception that was thrown. |
Definition at line 369 of file WThreadedFunction.h.
References WSharedObject< T >::getWriteTicket().
Referenced by WThreadedFunction< Function_T >::WThreadedFunction().
|
private |
WThreadedFunction is non-copyable, so the copy operator is not implemented.
|
virtual |
Starts the threads.
Implements WThreadedFunctionBase.
Definition at line 299 of file WThreadedFunction.h.
Referenced by WMTeemGlyphs::renderSlice(), WThreadedFunctionTest::testExceptionHandling(), WThreadedFunctionTest::testMultipleThreads(), WThreadedPerVoxelOperationTest::testMultithreadedFunction(), WThreadedFunctionTest::testStopCondition(), and WThreadedFunctionTest::testStopThreads().
|
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 313 of file WThreadedFunction.h.
Referenced by WThreadedFunctionTest::testStopThreads().
|
virtual |
Wait for all threads to stop.
Implements WThreadedFunctionBase.
Definition at line 327 of file WThreadedFunction.h.
Referenced by WMTeemGlyphs::renderSlice(), WThreadedFunctionTest::testExceptionHandling(), WThreadedFunctionTest::testMultipleThreads(), WThreadedPerVoxelOperationTest::testMultithreadedFunction(), and WThreadedFunctionTest::testStopThreads().
|
private |
the function object
Definition at line 250 of file WThreadedFunction.h.
Referenced by WThreadedFunction< Function_T >::WThreadedFunction().
|
private |
the number of threads to manage
Definition at line 243 of file WThreadedFunction.h.
Referenced by WThreadedFunction< Function_T >::WThreadedFunction().
|
private |
the threads
Definition at line 247 of file WThreadedFunction.h.
Referenced by WThreadedFunction< Function_T >::WThreadedFunction().
|
private |
a counter that keeps track of how many threads have finished
Definition at line 253 of file WThreadedFunction.h.
Referenced by WThreadedFunction< Function_T >::WThreadedFunction().