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

Last change on this file since 420 was 420, checked in by Torben Dannhauer, 11 years ago
File size: 1.5 KB
Line 
1#include "ChannelWorker.h"
2#include <osg/Notify>
3
4
5ChannelWorker::ChannelWorker(int numThreads, OpenThreads::Barrier& syncBarrier)
6 : _numThreads(numThreads), _syncBarrier(syncBarrier), _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
32        //_syncBarrier.block(_numThreads);  // Sync the threads.
33
34}
35
36void ChannelWorker::setDone(bool done)
37{
38        _done = done;
39}
40
41int ChannelWorker::cancel()
42{
43    OSG_ALWAYS<<"Cancelling ChannelWorker "<<this<<" isRunning()="<<isRunning()<<std::endl;
44
45    if( isRunning() )
46    {
47        _done = true;
48
49        OSG_ALWAYS<<"   Doing cancel "<<this<<std::endl;
50
51        // then wait for the the thread to stop running.
52        while(isRunning())
53        {
54            // commenting out debug info as it was cashing crash on exit, presumable
55            // due to OSG_NOTIFY or std::cout destructing earlier than this destructor.
56            OSG_ALWAYS<<"   Waiting for ChannelWorker to cancel "<<this<<std::endl;
57            OpenThreads::Thread::YieldCurrentThread();
58        }
59    }
60
61    OSG_ALWAYS<<"  ChannelWorker::cancel() thread cancelled "<<this<<" isRunning()="<<isRunning()<<std::endl;
62
63    return 0;
64}
Note: See TracBrowser for help on using the repository browser.