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

Last change on this file since 425 was 425, checked in by Torben Dannhauer, 12 years ago
File size: 1.9 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 IG Connectors
36        OSG_ALWAYS<<"Creating IG Connectors..."<<std::endl;
37        for(int i=0; i<numThreads-1; ++i)
38        {
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);
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
57void SimHost::run()
58{
59        // Launch threads
60    for(int i=0;i<static_cast<int>(_threadObjects.size());++i)
61        {
62                int status;
63               
64                //thread->setStackSize(1024*256);
65                status = _threadObjects[i]->start();
66                assert(status == 0);
67        }
68
69        // Do something in the main thread
70                for(int i=0;i<10000000;i++)     OSG_ALWAYS<<"";
71       
72        // Signal threads to finish
73    for(int i=0;i<static_cast<int>(_threadObjects.size());++i)
74        {
75                //_threadObjects[i]->setDone(true);     // non blocking
76                _threadObjects[i]->cancel();    // Blocking
77        }
78
79        for(int i=0;i<100000000;i++)
80                OSG_ALWAYS<<"";
81
82}
Note: See TracBrowser for help on using the repository browser.