25 #ifndef WTHREADEDFUNCTION_H
26 #define WTHREADEDFUNCTION_H
34 #include <boost/thread.hpp>
38 #include "WWorkerThread.h"
39 #include "WSharedObject.h"
45 enum WThreadedFunctionStatus
47 W_THREADS_INITIALIZED,
49 W_THREADS_STOP_REQUESTED,
57 enum WThreadedFunctionNbThreads
59 W_AUTOMATIC_NB_THREADS = 0
91 virtual void run() = 0;
97 virtual void stop() = 0;
102 virtual void wait() = 0;
109 WThreadedFunctionStatus
status();
176 template<
class Function_T >
194 WThreadedFunction( std::size_t numThreads, boost::shared_ptr< Function_T >
function );
249 std::vector< boost::shared_ptr< WWorkerThread< Function_T > > >
m_threads;
258 template<
class Function_T >
261 m_numThreads( numThreads ),
268 throw WException( std::string(
"No valid thread function pointer." ) );
294 template<
class Function_T >
300 template<
class Function_T >
304 m_threadsDone.getWriteTicket()->get() = 0;
306 m_status.getWriteTicket()->get() = W_THREADS_RUNNING;
308 for( std::size_t k = 0; k < m_numThreads; ++k )
310 m_threads[ k ]->run();
314 template<
class Function_T >
318 m_status.getWriteTicket()->get() = W_THREADS_STOP_REQUESTED;
320 typename std::vector< boost::shared_ptr< WWorkerThread< Function_T > > >::iterator it;
322 for( it = m_threads.begin(); it != m_threads.end(); ++it )
324 ( *it )->requestStop();
328 template<
class Function_T >
331 typename std::vector< boost::shared_ptr< WWorkerThread< Function_T > > >::iterator it;
333 for( it = m_threads.begin(); it != m_threads.end(); ++it )
339 template<
class Function_T >
345 WAssert( t->get() < m_numThreads,
"" );
347 std::size_t k = t->get();
350 if( m_numThreads == k )
354 if( s->get() == W_THREADS_RUNNING )
356 s->get() = W_THREADS_FINISHED;
358 else if( s->get() == W_THREADS_STOP_REQUESTED )
360 s->get() = W_THREADS_ABORTED;
364 throw WException( std::string(
"Invalid status change." ) );
366 m_doneCondition->notify();
370 template<
class Function_T >
376 WAssert( w->get() != W_THREADS_FINISHED &&
377 w->get() != W_THREADS_ABORTED,
"" );
378 if( w->get() == W_THREADS_RUNNING )
380 w->get() = W_THREADS_STOP_REQUESTED;
387 m_exceptionSignal( e );
390 #endif // WTHREADEDFUNCTION_H
Creates threads that computes a function in a multithreaded fashion.
A worker thread that belongs to a.
boost::signals2::signal< void(WException const &) > ExceptionSignal
a type for exception signals
std::vector< boost::shared_ptr< WWorkerThread< Function_T > > > m_threads
the threads
virtual void run()
Starts the threads.
A virtual base class for threaded functions (see below).
boost::shared_ptr< Function_T > m_func
the function object
ExceptionSignal m_exceptionSignal
a signal for exceptions
void handleThreadException(WException const &e)
This function handles exceptions thrown in the worker threads.
void handleThreadDone()
This function gets subscribed to the threads' stop signals.
virtual ~WThreadedFunctionBase()
Destroys the thread pool and stops all threads, if any one of them is still running.
virtual ~WThreadedFunction()
Destroys the thread pool and stops all threads, if any one of them is still running.
WThreadedFunction(std::size_t numThreads, boost::shared_ptr< Function_T > function)
Creates the thread pool with a given number of threads.
WriteTicket getWriteTicket(bool suppressNotify=false) const
Returns a ticket to get write access to the contained data.
WThreadedFunctionBase & operator=(WThreadedFunctionBase const &)
WThreadedFunctionBase is non-copyable, so the copy operator is not implemented.
virtual void stop()=0
Request all threads to stop.
WSharedObject< std::size_t > m_threadsDone
a counter that keeps track of how many threads have finished
boost::shared_ptr< WCondition > getThreadsDoneCondition()
Returns a condition that gets fired when all threads have finished.
WThreadedFunctionBase()
Standard constructor.
virtual void wait()=0
Wait for all threads to stop.
WThreadedFunctionStatus status()
Get the status of the threads.
boost::signals2::signal< void(WException const &) > ExceptionSignal
a type for exception signals
boost::function< void(WException const &) > ExceptionFunction
a type for exception callbacks
boost::shared_ptr< WCondition > m_doneCondition
a condition that gets notified when the work is complete
WSharedObject< WThreadedFunctionStatus > m_status
the current status
void subscribeExceptionSignal(ExceptionFunction func)
Subscribe a function to an exception signal.
std::size_t m_numThreads
the number of threads to manage
virtual void wait()
Wait for all threads to stop.
virtual void stop()
Request all threads to stop.
WThreadedFunction & operator=(WThreadedFunction const &)
WThreadedFunction is non-copyable, so the copy operator is not implemented.
boost::function< void(WException const &) > ExceptionFunction
a type for exception callbacks
virtual void run()=0
Starts the threads.