#pragma once #include #include #include #include #include "ThreadWorkerBase.h" /** * \brief This class is a Thread Object which allows to create Functors and attach them to this thread object. * * Subclass ThreadWorkerBase to implement your own functor to execute your custom code threaded. * * Use setDone(true) to shut down the thread nonblocking, or call cancel() to shut down the thread blocking (both called from outside of the workerthread). * Inside the workerthread's functor, you can set the functor parameter done=true to shutdown the thread after the current execution. * * * @author Torben Dannhauer * @date Okt 2012 */ class ThreadedWorker : public osg::Referenced, public OpenThreads::Thread { public: ThreadedWorker() ; ~ThreadedWorker(); //Destroying the thread object will implicit call cancel(), then the function will block until the thread has finished. virtual void run(); virtual int cancel(); void setDone(bool done); // signals the thread to stop but returns immediately. Use cancel if you want to wait blocking until the thread is canceled. bool getDone() const { return _done; } void setThreadWorker( ThreadWorkerBase* tworker); // This function sets the functor which should be executed threaded. Set NULL to remove the current functor and cancel the thread. ThreadWorkerBase* getCurrentThreadWorker() {return(_threadWorker);}; private: bool _done; osg::ref_ptr _threadWorker; OpenThreads::Mutex _threadWorkerMutex; // Ensures the functor is not replacable while it is executed };