source: experimental/Threading/Threading/ThreadedWorker.cpp @ 424

Last change on this file since 424 was 424, checked in by Torben Dannhauer, 7 years ago
File size: 1.4 KB
Line 
1#include "ThreadedWorker.h"
2#include <osg/Notify>
3
4
5ThreadedWorker::ThreadedWorker()
6 : _done(false)
7{
8        OSG_ALWAYS<<"ThreadedWorker instantiated."<<std::endl;
9}
10
11ThreadedWorker::~ThreadedWorker()
12{
13        cancel();
14}
15
16void ThreadedWorker::run()
17{
18        OSG_ALWAYS<<"ID "<<getThreadId()<<" running!"<<std::endl;
19
20        // Do the work
21        int i=0;
22        do
23        {
24                OSG_ALWAYS<<"ID "<<getThreadId()<<" :: itr "<<i++<<std::endl;
25
26                // Place Holder
27                for(int i=0;i<100;i++)
28                OSG_ALWAYS<<"";
29
30        } while (!testCancel() && !_done);      // repeat as long it is canceld or set as Done
31}
32
33void ThreadedWorker::setDone(bool done)
34{
35        _done = done;
36}
37
38int ThreadedWorker::cancel()
39{
40    OSG_ALWAYS<<"Cancelling ThreadedWorker "<<this<<" isRunning()="<<isRunning()<<std::endl;
41
42    if( isRunning() )
43    {
44        _done = true;
45
46        OSG_ALWAYS<<"   Doing cancel "<<this<<std::endl;
47
48        // then wait for the the thread to stop running.
49        while(isRunning())
50        {
51            // commenting out debug info as it was cashing crash on exit, presumable
52            // due to OSG_NOTIFY or std::cout destructing earlier than this destructor.
53            //OSG_ALWAYS<<"   Waiting for ThreadedWorker to cancel "<<this<<std::endl;
54            OpenThreads::Thread::YieldCurrentThread();
55        }
56    }
57
58    OSG_ALWAYS<<"  ThreadedWorker::cancel() thread cancelled "<<this<<" isRunning()="<<isRunning()<<std::endl;
59
60    return 0;
61}
Note: See TracBrowser for help on using the repository browser.