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

Last change on this file since 420 was 420, checked in by Torben Dannhauer, 11 years ago
File size: 1.8 KB
Line 
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
20SimHost::SimHost()
21{
22        OSG_ALWAYS<<"SimHost instantiated."<<std::endl;
23}
24
25SimHost::~SimHost()
26{
27        _channelWorker.clear();
28}
29
30void 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        {
39                osg::ref_ptr<ChannelWorker> worker = new ChannelWorker(numThreads, syncBarrier);
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
54void SimHost::run()
55{
56        // Launch threads
57    for(int i=0;i<static_cast<int>(_channelWorker.size());++i)
58        {
59                int status;
60                       
61                //thread->addObserver(&observer);
62                //thread->setStackSize(1024*256);
63                status = _channelWorker[i]->start();
64                assert(status == 0);
65        }
66
67        // Do something in the main thread
68        for(int i=0;i<10000000;i++)
69                OSG_ALWAYS<<"";
70       
71        // Signal threads to finish
72    for(int i=0;i<static_cast<int>(_channelWorker.size());++i)
73        {
74                _channelWorker[i]->setDone(true);
75                //_channelWorker[i]->cancel();          // Seems to be wrong
76        }
77
78        //syncBarrier.block(numThreads);  // Block until all threads are ready
79}
Note: See TracBrowser for help on using the repository browser.