25 #ifndef WTHREADEDFUNCTION_H
26 #define WTHREADEDFUNCTION_H
33 #include <boost/thread.hpp>
36 #include "WWorkerThread.h"
37 #include "WSharedObject.h"
38 #include "WExportCommon.h"
43 enum WThreadedFunctionStatus
45 W_THREADS_INITIALIZED,
47 W_THREADS_STOP_REQUESTED,
55 enum WThreadedFunctionNbThreads
57 W_AUTOMATIC_NB_THREADS = 0
90 virtual void run() = 0;
96 virtual void stop() = 0;
101 virtual void wait() = 0;
108 WThreadedFunctionStatus status();
115 boost::shared_ptr< WCondition > getThreadsDoneCondition();
175 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