Changeset 425 for experimental/Threading
- Timestamp:
- Oct 21, 2012, 12:05:47 AM (12 years ago)
- Location:
- experimental/Threading/Threading
- Files:
-
- 2 added
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
experimental/Threading/Threading/SimHost.cpp
r421 r425 33 33 OpenThreads::Barrier syncBarrier; 34 34 35 // Create ChannelWorkers36 OSG_ALWAYS<<"Creating ChannelWorkers..."<<std::endl;35 // Create IG Connectors 36 OSG_ALWAYS<<"Creating IG Connectors..."<<std::endl; 37 37 for(int i=0; i<numThreads-1; ++i) 38 38 { 39 osg::ref_ptr<ChannelWorker> worker = new ChannelWorker(); 40 _channelWorker.push_back(worker); 39 osg::ref_ptr<ThreadedWorker> threadObject = new ThreadedWorker(); 40 osg::ref_ptr<IGConnector> IGCon = new IGConnector(); 41 threadObject->setThreadWorker(IGCon); 42 _threadObjects.push_back(threadObject); 43 _IGConnectors.push_back(IGCon); 41 44 } 42 45 OSG_ALWAYS<<"...done."<<std::endl; … … 55 58 { 56 59 // Launch threads 57 for(int i=0;i<static_cast<int>(_ channelWorker.size());++i)60 for(int i=0;i<static_cast<int>(_threadObjects.size());++i) 58 61 { 59 62 int status; 60 63 61 64 //thread->setStackSize(1024*256); 62 status = _ channelWorker[i]->start();65 status = _threadObjects[i]->start(); 63 66 assert(status == 0); 64 67 } 65 68 66 69 // Do something in the main thread 67 for(int i=0;i<10000000;i++) 68 OSG_ALWAYS<<""; 70 for(int i=0;i<10000000;i++) OSG_ALWAYS<<""; 69 71 70 72 // Signal threads to finish 71 for(int i=0;i<static_cast<int>(_ channelWorker.size());++i)73 for(int i=0;i<static_cast<int>(_threadObjects.size());++i) 72 74 { 73 //_ channelWorker[i]->setDone(true); // non blocking74 _ channelWorker[i]->cancel(); // Blocking75 //_threadObjects[i]->setDone(true); // non blocking 76 _threadObjects[i]->cancel(); // Blocking 75 77 } 76 78 … … 78 80 OSG_ALWAYS<<""; 79 81 80 //syncBarrier.block(numThreads); // Block until all threads are ready81 82 } -
experimental/Threading/Threading/SimHost.h
r420 r425 5 5 #include <osg/ref_ptr> 6 6 7 #include "ChannelWorker.h" 7 #include "ThreadedWorker.h" 8 #include "IGConnector.h" 8 9 9 10 … … 18 19 19 20 private: 20 std::vector<osg::ref_ptr<ChannelWorker> > _channelWorker; 21 std::vector<osg::ref_ptr<IGConnector> > _IGConnectors; 22 std::vector<osg::ref_ptr<ThreadedWorker> > _threadObjects; 21 23 22 24 }; -
experimental/Threading/Threading/ThreadWorkerBase.h
r422 r425 1 1 #pragma once 2 #include <osg/Referenced> 3 4 5 class ThreadedWorker; 2 6 3 7 /** … … 5 9 * 6 10 * Just overload the operator() with your code you want to execute threaded in a loop. 7 * Each execution will be completed by the thread, if the thread is cancels during a run, the cancelation will be postponed until the executionof the functor is completed.11 * Each execution will be completed by the thread, if the thread is set as done/canceled during a run, the cancelation will be postponed until the execution of the functor is completed. 8 12 * 9 * Note: This functor will be executed in a loop until the thread is canceled or the parameter done is set totrue.13 * Note: This functor will be executed in a loop until the thread is canceled or set as done=true. 10 14 * 11 15 * @author Torben Dannhauer 12 16 * @date Okt 2012 13 17 */ 14 class ThreadWorkerBase 18 class ThreadWorkerBase : public osg::Referenced 15 19 { 16 20 public: … … 18 22 virtual ~ThreadWorkerBase() ; 19 23 20 void operator() (bool& _threadDone); 24 virtual void operator() (ThreadedWorker* threadObject) = 0; 25 26 27 21 28 }; -
experimental/Threading/Threading/ThreadedWorker.cpp
r424 r425 1 1 #include "ThreadedWorker.h" 2 2 #include <osg/Notify> 3 #include <OpenThreads/ScopedLock> 3 4 4 5 … … 19 20 20 21 // Do the work 21 int i=0;22 22 do 23 23 { 24 O SG_ALWAYS<<"ID "<<getThreadId()<<" :: itr "<<i++<<std::endl;24 OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_threadWorkerMutex); 25 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 26 if( _threadWorker.valid() ) 27 (*_threadWorker)(this); 28 29 } while (_threadWorker!=NULL && !testCancel() && !_done); // repeat as long as a functor is set and it is not canceled or set as Done 31 30 } 32 31 … … 60 59 return 0; 61 60 } 61 62 void ThreadedWorker::setThreadWorker( ThreadWorkerBase* tworker) 63 { 64 OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_threadWorkerMutex); 65 66 _threadWorker = tworker; 67 } -
experimental/Threading/Threading/ThreadedWorker.h
r424 r425 2 2 3 3 #include <osg/Referenced> 4 #include <osg/ref_ptr> 4 5 #include <OpenThreads/Thread> 6 #include <OpenThreads/Mutex> 7 8 #include "ThreadWorkerBase.h" 9 5 10 6 11 /** 7 12 * \brief This class is a Thread Object which allows to create Functors and attach them to this thread object. 13 * 14 * Subclass ThreadWorkerBase to implement your own functor to execute your custom code threaded. 15 * 16 * Use setDone(true) to shut down the thread nonblocking, or call cancel() to shut down the thread blocking (both called from outside of the workerthread). 17 * Inside the workerthread's functor, you can set the functor parameter done=true to shutdown the thread after the current execution. 18 * 8 19 * 9 20 * @author Torben Dannhauer … … 18 29 virtual int cancel(); 19 30 20 void setDone(bool done); // si nbgals the thread to stop but returns immediately. Use cancel if you want to wait blocking until the thread is canceled.31 void setDone(bool done); // signals the thread to stop but returns immediately. Use cancel if you want to wait blocking until the thread is canceled. 21 32 bool getDone() const { return _done; } 33 34 void setThreadWorker( ThreadWorkerBase* tworker); // This function sets the functor which should be executed threaded. Set NULL to remove the current functor and cancel the thread. 35 ThreadWorkerBase* getCurrentThreadWorker() {return(_threadWorker);}; 22 36 23 37 private: 24 38 bool _done; 39 osg::ref_ptr<ThreadWorkerBase> _threadWorker; 40 OpenThreads::Mutex _threadWorkerMutex; // Ensures the functor is not replacable while it is executed 25 41 }; -
experimental/Threading/Threading/Threading.vcproj
r424 r425 182 182 > 183 183 <File 184 RelativePath=".\IGConnector.cpp" 185 > 186 </File> 187 <File 184 188 RelativePath=".\main.cpp" 185 189 > … … 204 208 > 205 209 <File 210 RelativePath=".\IGConnector.h" 211 > 212 </File> 213 <File 206 214 RelativePath=".\SimHost.h" 207 215 > -
experimental/Threading/Threading/main.cpp
r420 r425 9 9 int main( int argc, char **argv ) 10 10 { 11 12 11 // Create osgVisual::SimHost 13 OSG_ALWAYS<<"Creating SimHost..."<<std::endl;14 12 osg::ref_ptr<SimHost> simulationHost = new SimHost(); 15 OSG_ALWAYS<<"...done."<<std::endl;16 13 17 14 //Start it
Note: See TracChangeset
for help on using the changeset viewer.