Rev | Line | |
---|
[420] | 1 | #pragma once |
---|
| 2 | |
---|
| 3 | #include <osg/Referenced> |
---|
[425] | 4 | #include <osg/ref_ptr> |
---|
[420] | 5 | #include <OpenThreads/Thread> |
---|
[425] | 6 | #include <OpenThreads/Mutex> |
---|
[420] | 7 | |
---|
[425] | 8 | #include "ThreadWorkerBase.h" |
---|
| 9 | |
---|
| 10 | |
---|
[422] | 11 | /** |
---|
| 12 | * \brief This class is a Thread Object which allows to create Functors and attach them to this thread object. |
---|
| 13 | * |
---|
[425] | 14 | * Subclass ThreadWorkerBase to implement your own functor to execute your custom code threaded. |
---|
| 15 | * |
---|
| 16 | * 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). |
---|
| 17 | * Inside the workerthread's functor, you can set the functor parameter done=true to shutdown the thread after the current execution. |
---|
| 18 | * |
---|
[426] | 19 | * Use osg::ref_ptr pointer to manage the thread object. |
---|
| 20 | * This causes the threadobjects to get deleted finally and hence the threads aret canceled formally -> The app won't finish until all threads have finished their work. |
---|
[425] | 21 | * |
---|
[422] | 22 | * @author Torben Dannhauer |
---|
| 23 | * @date Okt 2012 |
---|
| 24 | */ |
---|
[423] | 25 | class ThreadedWorker : public osg::Referenced, public OpenThreads::Thread |
---|
[420] | 26 | { |
---|
| 27 | public: |
---|
[423] | 28 | ThreadedWorker() ; |
---|
| 29 | ~ThreadedWorker(); //Destroying the thread object will implicit call cancel(), then the function will block until the thread has finished. |
---|
[420] | 30 | virtual void run(); |
---|
| 31 | virtual int cancel(); |
---|
| 32 | |
---|
[425] | 33 | 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. |
---|
[421] | 34 | bool getDone() const { return _done; } |
---|
[420] | 35 | |
---|
[425] | 36 | 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. |
---|
| 37 | ThreadWorkerBase* getCurrentThreadWorker() {return(_threadWorker);}; |
---|
| 38 | |
---|
[420] | 39 | private: |
---|
| 40 | bool _done; |
---|
[425] | 41 | osg::ref_ptr<ThreadWorkerBase> _threadWorker; |
---|
| 42 | OpenThreads::Mutex _threadWorkerMutex; // Ensures the functor is not replacable while it is executed |
---|
[420] | 43 | }; |
---|
Note: See
TracBrowser
for help on using the repository browser.