#ifndef __IRUNNABLE__ #define __IRUNNABLE__ #include #include namespace Threading { class Runnable { public: Runnable():_running(true){} virtual void Run() = 0; virtual void Stop() { boost::mutex::scoped_lock lock(_runningMutex); _running = false; } bool ShouldContinue() { boost::mutex::scoped_lock lock(_runningMutex); return _running; } protected: // sleeps thread for x sec void Pause(int sec) { boost::xtime xt; boost::xtime_get(&xt, boost::TIME_UTC); xt.sec += sec; boost::thread::sleep(xt); } void Pause(int sec, int ns) { boost::xtime xt; boost::xtime_get(&xt, boost::TIME_UTC); xt.sec += sec; xt.nsec += ns*1000; boost::thread::sleep(xt); } private: boost::mutex _runningMutex; bool _running; }; // used to pass the Runnable object to the thread's start function struct boost_thread_starter { boost_thread_starter(Runnable* runnable):_runnable(runnable){} // function is called when boost::thread starts void operator()() { // start the Runnable's run method _runnable->Run(); } Runnable* _runnable; }; }// end Threading namespace #endif // __IRUNNABLE__