25 #ifndef WTHREADEDFUNCTION_H
26 #define WTHREADEDFUNCTION_H
33 #include <boost/thread.hpp>
36 #include "WWorkerThread.h"
37 #include "WSharedObject.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;
95 virtual void stop() = 0;
100 virtual void wait() = 0;
107 WThreadedFunctionStatus
status();
174 template<
class Function_T >
192 WThreadedFunction( std::size_t numThreads, boost::shared_ptr< Function_T >
function );
247 std::vector< boost::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< boost::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< boost::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 );
388 #endif // WTHREADEDFUNCTION_H