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

Last change on this file since 421 was 421, checked in by Torben Dannhauer, 6 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();
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->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        {
73                //_channelWorker[i]->setDone(true);     // non blocking
74                _channelWorker[i]->cancel();    // Blocking
75        }
76
77        for(int i=0;i<100000000;i++)
78                OSG_ALWAYS<<"";
79
80        //syncBarrier.block(numThreads);  // Block until all threads are ready
81}
Note: See TracBrowser for help on using the repository browser.