source: experimental/Threading/Threading/SimHost.cpp @ 430

Last change on this file since 430 was 430, checked in by Torben Dannhauer, 11 years ago
File size: 2.0 KB
Line 
1#include "SimHost.h"
2#include <osg/Notify>
3
4#include <OpenThreads/Thread>
5
6// System includes
7#include <cassert>
8
9#ifdef _WIN32
10#include <process.h>
11#define getpid() _getpid()
12#else
13#include <unistd.h>
14#endif
15
16// App includes
17
18
19SimHost::SimHost()
20{
21        OSG_ALWAYS<<"SimHost instantiated."<<std::endl;
22        numberofChannels = 6;
23}
24
25SimHost::~SimHost()
26{
27        //_channelWorker.clear();
28}
29
30void SimHost::initialize()
31{
32        done = false;
33        int numThreads = numberofChannels+1;    // #channels + one main thread
34        OpenThreads::Barrier syncBarrier;
35
36        // Create IG Connectors
37        OSG_ALWAYS<<"Creating IG Connectors..."<<std::endl;
38        for(int i=0; i<numberofChannels; ++i)
39        {
40                osg::ref_ptr<ThreadedWorker> threadObject = new ThreadedWorker();
41                osg::ref_ptr<IGConnector> IGCon = new IGConnector(numberofChannels, _frameBarrier);
42                threadObject->setThreadWorker(IGCon);
43                _threadObjects.push_back(threadObject);
44                _IGConnectors.push_back(IGCon);
45        }
46        OSG_ALWAYS<<"...done."<<std::endl;
47
48        // Setup Threading
49        OSG_ALWAYS << "Root Thread ID: " << getpid() << std::endl;
50
51        OSG_ALWAYS<<OpenThreads::GetNumberOfProcessors()<<" CPUs found, using "<<(numThreads)<<" of them concurrently."<<std::endl;
52        OpenThreads::Thread::SetConcurrency( numThreads );      // Only supported by pthread implementation, otherwise ignored
53
54        OpenThreads::Thread::Init();
55       
56}
57
58void SimHost::run()
59{
60        // Launch threads
61    for(int i=0;i<static_cast<int>(_threadObjects.size());++i)
62        {
63                int status;
64               
65                //thread->setStackSize(1024*256);
66                status = _threadObjects[i]->start();
67                assert(status == 0);
68        }
69
70        // Enter the SimHost main loop
71        while(!done)
72        {
73                // Wait until all Channels have sent an SOF packet
74                _frameBarrier.block(numberofChannels+1);  // Sync the main and all IG threads.
75                OSG_ALWAYS<<"All IGConnectors have recieved a SOF packet: Starting new Frame"<<std::endl;
76        }
77       
78        // Signal threads to finish
79    for(int i=0;i<static_cast<int>(_threadObjects.size());++i)
80        {
81                //_threadObjects[i]->setDone(true);     // non blocking
82                _threadObjects[i]->cancel();    // Blocking
83        }
84}
Note: See TracBrowser for help on using the repository browser.