OpenWalnut  1.5.0dev
Public Types | Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
WThreadedFunction< Function_T > Class Template Reference

Creates threads that computes a function in a multithreaded fashion. More...

#include <WThreadedFunction.h>

+ Inheritance diagram for WThreadedFunction< Function_T >:
+ Collaboration diagram for WThreadedFunction< Function_T >:

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< WConditiongetThreadsDoneCondition ()
 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...
 
WThreadedFunctionoperator= (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...
 
WThreadedFunctionBaseoperator= (WThreadedFunctionBase const &)
 WThreadedFunctionBase is non-copyable, so the copy operator is not implemented. More...
 
- Protected Attributes inherited from WThreadedFunctionBase
std::shared_ptr< WConditionm_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...
 

Detailed Description

template<class Function_T>
class WThreadedFunction< Function_T >

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.

Member Typedef Documentation

◆ ExceptionFunction

template<class Function_T >
typedef boost::function< void ( WException const& ) > WThreadedFunction< Function_T >::ExceptionFunction

a type for exception callbacks

Definition at line 182 of file WThreadedFunction.h.

◆ ExceptionSignal

template<class Function_T >
typedef boost::signals2::signal< void ( WException const& ) > WThreadedFunction< Function_T >::ExceptionSignal
private

a type for exception signals

Definition at line 178 of file WThreadedFunction.h.

Constructor & Destructor Documentation

◆ WThreadedFunction() [1/2]

template<class Function_T >
WThreadedFunction< Function_T >::WThreadedFunction ( std::size_t  numThreads,
std::shared_ptr< Function_T >  function 
)

Creates the thread pool with a given number of threads.

Parameters
numThreadsThe number of threads to create.
functionThe function object.
Note
If the number of threads equals 0, a good number of threads will be determined by the threadpool.

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.

+ Here is the call graph for this function:

◆ ~WThreadedFunction()

template<class Function_T >
WThreadedFunction< Function_T >::~WThreadedFunction
virtual

Destroys the thread pool and stops all threads, if any one of them is still running.

Note
Of course, the client has to make sure the threads do not work endlessly on a single job.

Definition at line 293 of file WThreadedFunction.h.

◆ WThreadedFunction() [2/2]

template<class Function_T >
WThreadedFunction< Function_T >::WThreadedFunction ( WThreadedFunction< Function_T > const &  )
private

WThreadedFunction is non-copyable, so the copy constructor is not implemented.

Member Function Documentation

◆ handleThreadDone()

template<class Function_T >
void WThreadedFunction< Function_T >::handleThreadDone
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().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ handleThreadException()

template<class Function_T >
void WThreadedFunction< Function_T >::handleThreadException ( WException const &  e)
private

This function handles exceptions thrown in the worker threads.

Parameters
eThe exception that was thrown.

Definition at line 369 of file WThreadedFunction.h.

References WSharedObject< T >::getWriteTicket().

Referenced by WThreadedFunction< Function_T >::WThreadedFunction().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ operator=()

template<class Function_T >
WThreadedFunction& WThreadedFunction< Function_T >::operator= ( WThreadedFunction< Function_T > const &  )
private

WThreadedFunction is non-copyable, so the copy operator is not implemented.

Returns
this function

◆ run()

template<class Function_T >
void WThreadedFunction< Function_T >::run
virtual

◆ stop()

template<class Function_T >
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 313 of file WThreadedFunction.h.

Referenced by WThreadedFunctionTest::testStopThreads().

+ Here is the caller graph for this function:

◆ wait()

template<class Function_T >
void WThreadedFunction< Function_T >::wait
virtual

Member Data Documentation

◆ m_func

template<class Function_T >
std::shared_ptr< Function_T > WThreadedFunction< Function_T >::m_func
private

the function object

Definition at line 250 of file WThreadedFunction.h.

Referenced by WThreadedFunction< Function_T >::WThreadedFunction().

◆ m_numThreads

template<class Function_T >
std::size_t WThreadedFunction< Function_T >::m_numThreads
private

the number of threads to manage

Definition at line 243 of file WThreadedFunction.h.

Referenced by WThreadedFunction< Function_T >::WThreadedFunction().

◆ m_threads

template<class Function_T >
std::vector< std::shared_ptr< WWorkerThread< Function_T > > > WThreadedFunction< Function_T >::m_threads
private

the threads

Definition at line 247 of file WThreadedFunction.h.

Referenced by WThreadedFunction< Function_T >::WThreadedFunction().

◆ m_threadsDone

template<class Function_T >
WSharedObject< std::size_t > WThreadedFunction< Function_T >::m_threadsDone
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().


The documentation for this class was generated from the following file: