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

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