[420] | 1 | #include "SimHost.h" |
---|
| 2 | #include <osg/Notify> |
---|
| 3 | |
---|
| 4 | #include <OpenThreads/Thread> |
---|
| 5 | #include <OpenThreads/Barrier> |
---|
| 6 | |
---|
| 7 | // System includes |
---|
| 8 | #include <cassert> |
---|
| 9 | |
---|
| 10 | #ifdef _WIN32 |
---|
| 11 | #include <process.h> |
---|
| 12 | #define getpid() _getpid() |
---|
| 13 | #else |
---|
| 14 | #include <unistd.h> |
---|
| 15 | #endif |
---|
| 16 | |
---|
| 17 | // App includes |
---|
| 18 | |
---|
| 19 | |
---|
| 20 | SimHost::SimHost() |
---|
| 21 | { |
---|
| 22 | OSG_ALWAYS<<"SimHost instantiated."<<std::endl; |
---|
| 23 | } |
---|
| 24 | |
---|
| 25 | SimHost::~SimHost() |
---|
| 26 | { |
---|
[421] | 27 | //_channelWorker.clear(); |
---|
[420] | 28 | } |
---|
| 29 | |
---|
| 30 | void SimHost::initialize() |
---|
| 31 | { |
---|
| 32 | int numThreads = 6+1; // 6 channels and one main thread |
---|
| 33 | OpenThreads::Barrier syncBarrier; |
---|
| 34 | |
---|
[425] | 35 | // Create IG Connectors |
---|
| 36 | OSG_ALWAYS<<"Creating IG Connectors..."<<std::endl; |
---|
[420] | 37 | for(int i=0; i<numThreads-1; ++i) |
---|
| 38 | { |
---|
[425] | 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); |
---|
[420] | 44 | } |
---|
| 45 | OSG_ALWAYS<<"...done."<<std::endl; |
---|
| 46 | |
---|
| 47 | // Setup Threading |
---|
| 48 | OSG_ALWAYS << "Root Thread ID: " << getpid() << std::endl; |
---|
| 49 | |
---|
| 50 | OSG_ALWAYS<<OpenThreads::GetNumberOfProcessors()<<" CPUs found, using "<<(numThreads)<<" of them concurrently."<<std::endl; |
---|
| 51 | OpenThreads::Thread::SetConcurrency( numThreads ); // Only supported by pthread implementation, otherwise ignored |
---|
| 52 | |
---|
| 53 | OpenThreads::Thread::Init(); |
---|
| 54 | |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | void SimHost::run() |
---|
| 58 | { |
---|
| 59 | // Launch threads |
---|
[425] | 60 | for(int i=0;i<static_cast<int>(_threadObjects.size());++i) |
---|
[420] | 61 | { |
---|
| 62 | int status; |
---|
[421] | 63 | |
---|
[420] | 64 | //thread->setStackSize(1024*256); |
---|
[425] | 65 | status = _threadObjects[i]->start(); |
---|
[420] | 66 | assert(status == 0); |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | // Do something in the main thread |
---|
[425] | 70 | for(int i=0;i<10000000;i++) OSG_ALWAYS<<""; |
---|
[420] | 71 | |
---|
| 72 | // Signal threads to finish |
---|
[425] | 73 | for(int i=0;i<static_cast<int>(_threadObjects.size());++i) |
---|
[420] | 74 | { |
---|
[425] | 75 | //_threadObjects[i]->setDone(true); // non blocking |
---|
| 76 | _threadObjects[i]->cancel(); // Blocking |
---|
[420] | 77 | } |
---|
| 78 | |
---|
[421] | 79 | for(int i=0;i<100000000;i++) |
---|
| 80 | OSG_ALWAYS<<""; |
---|
| 81 | |
---|
[420] | 82 | } |
---|