source: experimental/Threading/Threading/ChannelWorker.cpp @ 422

Last change on this file since 422 was 422, checked in by Torben Dannhauer, 12 years ago
File size: 1.4 KB
Line 
1#include "ChannelWorker.h"
2#include <osg/Notify>
3
4
5ChannelWorker::ChannelWorker()
6 : _done(false)
7{
8        OSG_ALWAYS<<"ChannelWorker instantiated."<<std::endl;
9}
10
11ChannelWorker::~ChannelWorker()
12{
13        cancel();
14}
15
16void ChannelWorker::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 ChannelWorker::setDone(bool done)
34{
35        _done = done;
36}
37
38int ChannelWorker::cancel()
39{
40    OSG_ALWAYS<<"Cancelling ChannelWorker "<<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 ChannelWorker to cancel "<<this<<std::endl;
54            OpenThreads::Thread::YieldCurrentThread();
55        }
56    }
57
58    OSG_ALWAYS<<"  ChannelWorker::cancel() thread cancelled "<<this<<" isRunning()="<<isRunning()<<std::endl;
59
60    return 0;
61}
Note: See TracBrowser for help on using the repository browser.