OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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 >:

Public Types

typedef boost::function< void(WException
const &) > 
ExceptionFunction
 a type for exception callbacks
- Public Types inherited from WThreadedFunctionBase
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.
- Public Member Functions inherited from WThreadedFunctionBase
 WThreadedFunctionBase ()
 Standard constructor.
virtual ~WThreadedFunctionBase ()
 Destroys the thread pool and stops all threads, if any one of them is still running.
WThreadedFunctionStatus status ()
 Get the status of the threads.
boost::shared_ptr< WConditiongetThreadsDoneCondition ()
 Returns a condition that gets fired when all threads have finished.
void subscribeExceptionSignal (ExceptionFunction func)
 Subscribe a function to an exception signal.

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.
WThreadedFunctionoperator= (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

Additional Inherited Members

- Protected Member Functions inherited from WThreadedFunctionBase
 WThreadedFunctionBase (WThreadedFunctionBase const &)
 WThreadedFunctionBase is non-copyable, so the copy constructor is not implemented.
WThreadedFunctionBaseoperator= (WThreadedFunctionBase const &)
 WThreadedFunctionBase is non-copyable, so the copy operator is not implemented.
- Protected Attributes inherited from WThreadedFunctionBase
boost::shared_ptr< WConditionm_doneCondition
 a condition that gets notified when the work is complete
ExceptionSignal m_exceptionSignal
 a signal for exceptions
WSharedObject
< WThreadedFunctionStatus > 
m_status
 the current status

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 176 of file WThreadedFunction.h.

Member Typedef Documentation

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

a type for exception callbacks

Definition at line 184 of file WThreadedFunction.h.

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

a type for exception signals

Definition at line 179 of file WThreadedFunction.h.

Constructor & Destructor Documentation

template<class Function_T >
WThreadedFunction< Function_T >::WThreadedFunction ( std::size_t  numThreads,
boost::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.
Notes:
If the number of threads equals 0, a good number of threads will be determined by the threadpool.

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.

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.

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

Definition at line 295 of file WThreadedFunction.h.

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

template<class Function_T >
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().

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

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 371 of file WThreadedFunction.h.

References WSharedObject< T >::getWriteTicket().

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

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
template<class Function_T >
void WThreadedFunction< Function_T >::run ( )
virtual
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 315 of file WThreadedFunction.h.

Referenced by WThreadedFunctionTest::testStopThreads().

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

Member Data Documentation

template<class Function_T>
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().

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

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

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 255 of file WThreadedFunction.h.

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


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