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

Last change on this file since 421 was 421, 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                for(int i=0;i<100;i++)
27                OSG_ALWAYS<<"";
28
29        } while (!testCancel() && !_done);      // repeat as long it is canceld or set as Done
30}
31
32void ChannelWorker::setDone(bool done)
33{
34        _done = done;
35}
36
37int ChannelWorker::cancel()
38{
39    OSG_ALWAYS<<"Cancelling ChannelWorker "<<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 ChannelWorker to cancel "<<this<<std::endl;
53            OpenThreads::Thread::YieldCurrentThread();
54        }
55    }
56
57    OSG_ALWAYS<<"  ChannelWorker::cancel() thread cancelled "<<this<<" isRunning()="<<isRunning()<<std::endl;
58
59    return 0;
60}
Note: See TracBrowser for help on using the repository browser.