Line | |
---|
1 | #include "ThreadedWorker.h" |
---|
2 | #include <osg/Notify> |
---|
3 | #include <OpenThreads/ScopedLock> |
---|
4 | |
---|
5 | |
---|
6 | ThreadedWorker::ThreadedWorker() |
---|
7 | : _done(false) |
---|
8 | { |
---|
9 | OSG_ALWAYS<<"ThreadedWorker instantiated."<<std::endl; |
---|
10 | } |
---|
11 | |
---|
12 | ThreadedWorker::~ThreadedWorker() |
---|
13 | { |
---|
14 | cancel(); |
---|
15 | } |
---|
16 | |
---|
17 | void ThreadedWorker::run() |
---|
18 | { |
---|
19 | OSG_ALWAYS<<"ID "<<getThreadId()<<" running!"<<std::endl; |
---|
20 | |
---|
21 | // Do the work |
---|
22 | do |
---|
23 | { |
---|
24 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_threadWorkerMutex); |
---|
25 | |
---|
26 | if( _threadWorker.valid() ) |
---|
27 | (*_threadWorker)(this); |
---|
28 | |
---|
29 | } while (_threadWorker!=NULL && !testCancel() && !_done); // repeat as long as a functor is set and it is not canceled or set as Done |
---|
30 | } |
---|
31 | |
---|
32 | void ThreadedWorker::setDone(bool done) |
---|
33 | { |
---|
34 | _done = done; |
---|
35 | } |
---|
36 | |
---|
37 | int ThreadedWorker::cancel() |
---|
38 | { |
---|
39 | OSG_ALWAYS<<"Cancelling ThreadedWorker "<<this<<" isRunning()="<<isRunning()<<std::endl; |
---|
40 | |
---|
41 | if( isRunning() ) |
---|
42 | { |
---|
43 | _done = true; |
---|
44 | |
---|
45 | OSG_ALWAYS<<" Doing cancel "<<this<<std::endl; |
---|
46 | |
---|
47 | // then wait for the the thread to stop running. |
---|
48 | while(isRunning()) |
---|
49 | { |
---|
50 | // commenting out debug info as it was cashing crash on exit, presumable |
---|
51 | // due to OSG_NOTIFY or std::cout destructing earlier than this destructor. |
---|
52 | //OSG_ALWAYS<<" Waiting for ThreadedWorker to cancel "<<this<<std::endl; |
---|
53 | OpenThreads::Thread::YieldCurrentThread(); |
---|
54 | } |
---|
55 | } |
---|
56 | |
---|
57 | OSG_ALWAYS<<" ThreadedWorker::cancel() thread cancelled "<<this<<" isRunning()="<<isRunning()<<std::endl; |
---|
58 | |
---|
59 | return 0; |
---|
60 | } |
---|
61 | |
---|
62 | void ThreadedWorker::setThreadWorker( ThreadWorkerBase* tworker) |
---|
63 | { |
---|
64 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_threadWorkerMutex); |
---|
65 | |
---|
66 | _threadWorker = tworker; |
---|
67 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.