OpenWalnut  1.4.0
Public Types | Public Member Functions | Private Types | Private Member Functions | Private Attributes
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 >:

List of all members.

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::signals2::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

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

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

a type for exception callbacks

Reimplemented from WThreadedFunctionBase.

Definition at line 182 of file WThreadedFunction.h.

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

a type for exception signals

Reimplemented from WThreadedFunctionBase.

Definition at line 178 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 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.

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 293 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 338 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 369 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 313 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 250 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 243 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 247 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 253 of file WThreadedFunction.h.

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


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