25 #ifndef WTHREADEDFUNCTION_H
26 #define WTHREADEDFUNCTION_H
33 #include <boost/thread.hpp>
36 #include "WSharedObject.h"
37 #include "WWorkerThread.h"
43 enum WThreadedFunctionStatus
45 W_THREADS_INITIALIZED,
47 W_THREADS_STOP_REQUESTED,
55 enum WThreadedFunctionNbThreads
57 W_AUTOMATIC_NB_THREADS = 0
89 virtual void run() = 0;
107 WThreadedFunctionStatus
status();
174 template<
class Function_T >
192 WThreadedFunction( std::size_t numThreads, std::shared_ptr< Function_T >
function );
247 std::vector< std::shared_ptr< WWorkerThread< Function_T > > >
m_threads;
256 template<
class Function_T >
259 m_numThreads( numThreads ),
266 throw WException( std::string(
"No valid thread function pointer." ) );
292 template<
class Function_T >
298 template<
class Function_T >
302 m_threadsDone.getWriteTicket()->get() = 0;
304 m_status.getWriteTicket()->get() = W_THREADS_RUNNING;
306 for( std::size_t k = 0; k < m_numThreads; ++k )
308 m_threads[ k ]->run();
312 template<
class Function_T >
316 m_status.getWriteTicket()->get() = W_THREADS_STOP_REQUESTED;
318 typename std::vector< std::shared_ptr< WWorkerThread< Function_T > > >::iterator it;
320 for( it = m_threads.begin(); it != m_threads.end(); ++it )
322 ( *it )->requestStop();
326 template<
class Function_T >
329 typename std::vector< std::shared_ptr< WWorkerThread< Function_T > > >::iterator it;
331 for( it = m_threads.begin(); it != m_threads.end(); ++it )
337 template<
class Function_T >
343 WAssert( t->get() < m_numThreads,
"" );
345 std::size_t k = t->get();
348 if( m_numThreads == k )
352 if( s->get() == W_THREADS_RUNNING )
354 s->get() = W_THREADS_FINISHED;
356 else if( s->get() == W_THREADS_STOP_REQUESTED )
358 s->get() = W_THREADS_ABORTED;
362 throw WException( std::string(
"Invalid status change." ) );
364 m_doneCondition->notify();
368 template<
class Function_T >
374 WAssert( w->get() != W_THREADS_FINISHED &&
375 w->get() != W_THREADS_ABORTED,
"" );
376 if( w->get() == W_THREADS_RUNNING )
378 w->get() = W_THREADS_STOP_REQUESTED;
385 m_exceptionSignal( e );
WriteTicket getWriteTicket(bool suppressNotify=false) const
Returns a ticket to get write access to the contained data.
A virtual base class for threaded functions (see below).
std::shared_ptr< WCondition > m_doneCondition
a condition that gets notified when the work is complete
std::shared_ptr< WCondition > getThreadsDoneCondition()
Returns a condition that gets fired when all threads have finished.
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
WThreadedFunctionBase & operator=(WThreadedFunctionBase const &)
WThreadedFunctionBase is non-copyable, so the copy operator is not implemented.
ExceptionSignal m_exceptionSignal
a signal for exceptions
virtual void run()=0
Starts the threads.
WThreadedFunctionBase()
Standard constructor.
void subscribeExceptionSignal(ExceptionFunction func)
Subscribe a function to an exception signal.
virtual ~WThreadedFunctionBase()
Destroys the thread pool and stops all threads, if any one of them is still running.
virtual void stop()=0
Request all threads to stop.
WSharedObject< WThreadedFunctionStatus > m_status
the current status
WThreadedFunctionBase(WThreadedFunctionBase const &)
WThreadedFunctionBase is non-copyable, so the copy constructor is not implemented.
virtual void wait()=0
Wait for all threads to stop.
Creates threads that computes a function in a multithreaded fashion.
WThreadedFunction(std::size_t numThreads, std::shared_ptr< Function_T > function)
Creates the thread pool with a given number of threads.
WSharedObject< std::size_t > m_threadsDone
a counter that keeps track of how many threads have finished
virtual void wait()
Wait for all threads to stop.
virtual void stop()
Request all threads to stop.
boost::function< void(WException const &) > ExceptionFunction
a type for exception callbacks
std::vector< std::shared_ptr< WWorkerThread< Function_T > > > m_threads
the threads
void handleThreadDone()
This function gets subscribed to the threads' stop signals.
virtual ~WThreadedFunction()
Destroys the thread pool and stops all threads, if any one of them is still running.
boost::signals2::signal< void(WException const &) > ExceptionSignal
a type for exception signals
WThreadedFunction & operator=(WThreadedFunction const &)
WThreadedFunction is non-copyable, so the copy operator is not implemented.
std::size_t m_numThreads
the number of threads to manage
std::shared_ptr< Function_T > m_func
the function object
void handleThreadException(WException const &e)
This function handles exceptions thrown in the worker threads.
WThreadedFunction(WThreadedFunction const &)
WThreadedFunction is non-copyable, so the copy constructor is not implemented.
virtual void run()
Starts the threads.
A worker thread that belongs to a.