[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 | |
---|
| 35 | // Create ChannelWorkers |
---|
| 36 | OSG_ALWAYS<<"Creating ChannelWorkers..."<<std::endl; |
---|
| 37 | for(int i=0; i<numThreads-1; ++i) |
---|
| 38 | { |
---|
[421] | 39 | osg::ref_ptr<ChannelWorker> worker = new ChannelWorker(); |
---|
[420] | 40 | _channelWorker.push_back(worker); |
---|
| 41 | } |
---|
| 42 | OSG_ALWAYS<<"...done."<<std::endl; |
---|
| 43 | |
---|
| 44 | // Setup Threading |
---|
| 45 | OSG_ALWAYS << "Root Thread ID: " << getpid() << std::endl; |
---|
| 46 | |
---|
| 47 | OSG_ALWAYS<<OpenThreads::GetNumberOfProcessors()<<" CPUs found, using "<<(numThreads)<<" of them concurrently."<<std::endl; |
---|
| 48 | OpenThreads::Thread::SetConcurrency( numThreads ); // Only supported by pthread implementation, otherwise ignored |
---|
| 49 | |
---|
| 50 | OpenThreads::Thread::Init(); |
---|
| 51 | |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | void SimHost::run() |
---|
| 55 | { |
---|
| 56 | // Launch threads |
---|
| 57 | for(int i=0;i<static_cast<int>(_channelWorker.size());++i) |
---|
| 58 | { |
---|
| 59 | int status; |
---|
[421] | 60 | |
---|
[420] | 61 | //thread->setStackSize(1024*256); |
---|
| 62 | status = _channelWorker[i]->start(); |
---|
| 63 | assert(status == 0); |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | // Do something in the main thread |
---|
| 67 | for(int i=0;i<10000000;i++) |
---|
| 68 | OSG_ALWAYS<<""; |
---|
| 69 | |
---|
| 70 | // Signal threads to finish |
---|
| 71 | for(int i=0;i<static_cast<int>(_channelWorker.size());++i) |
---|
| 72 | { |
---|
[421] | 73 | //_channelWorker[i]->setDone(true); // non blocking |
---|
| 74 | _channelWorker[i]->cancel(); // Blocking |
---|
[420] | 75 | } |
---|
| 76 | |
---|
[421] | 77 | for(int i=0;i<100000000;i++) |
---|
| 78 | OSG_ALWAYS<<""; |
---|
| 79 | |
---|
[420] | 80 | //syncBarrier.block(numThreads); // Block until all threads are ready |
---|
| 81 | } |
---|