25 #ifndef WWORKERTHREAD_H
26 #define WWORKERTHREAD_H
32 #include <boost/signals2/signal.hpp>
35 #include "WException.h"
36 #include "WThreadedRunner.h"
41 template<
class Function_T >
66 WWorkerThread( std::shared_ptr< Function_T > func, std::size_t
id, std::size_t numThreads );
122 template<
class Function_T >
126 m_numThreads( numThreads ),
130 if(
id >= numThreads )
132 throw WException( std::string(
"The id of this thread is not valid." ) );
136 throw WException( std::string(
"No thread function provided!" ) );
140 template<
class Function_T >
143 m_exceptionSignal.disconnect_all_slots();
144 m_stopSignal.disconnect_all_slots();
147 template<
class Function_T >
152 m_exceptionSignal.connect( func );
156 template<
class Function_T >
161 m_stopSignal.connect( func );
165 template<
class Function_T >
172 m_func->operator() ( m_id, m_numThreads, m_shutdownFlag );
176 m_exceptionSignal( e );
179 catch( std::exception
const& e )
182 m_exceptionSignal( w );
187 WException w( std::string(
"An exception was thrown." ) );
188 m_exceptionSignal( w );
virtual const char * what() const
Returns the message string set on throw.
Base class for all classes needing to be executed in a separate thread.
A worker thread that belongs to a.
WWorkerThread & operator=(WWorkerThread const &)
WWorkerThread is non-copyable, so the copy operator is not implemented.
boost::signals2::signal< void(WException const &) > ExceptionSignal
a type for exception signals
virtual void threadMain()
The thread's main function.
ExceptionSignal m_exceptionSignal
the exception signal
boost::function< void() > StopFunction
a type for stop callbacks
void subscribeStopSignal(StopFunction func)
Subscribe a function to the stop signal.
WWorkerThread(WWorkerThread const &)
WWorkerThread is non-copyable, so the copy constructor is not implemented.
boost::function< void(WException const &) > ExceptionFunction
a type for exception callbacks
void subscribeExceptionSignal(ExceptionFunction func)
Subscribe a function to the exception signal.
std::size_t m_id
a thread id between 0 and m_numThreads - 1
std::size_t m_numThreads
the number of threads
boost::signals2::signal< void() > StopSignal
a type for stop signals
std::shared_ptr< Function_T > m_func
the functor called in threadMain()
virtual ~WWorkerThread()
Default destructor.
WWorkerThread(std::shared_ptr< Function_T > func, std::size_t id, std::size_t numThreads)
Default constructor.
StopSignal m_stopSignal
the stop signal