xmsinterp  1.0
ThreadMgr.cpp
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
6 //------------------------------------------------------------------------------
7 
8 //----- Included files ---------------------------------------------------------
9 
10 // 1. Precompiled header
11 
12 // 2. My own header
14 
15 // 3. Standard library headers
16 
17 // 4. External library headers
18 #include <boost/thread.hpp>
19 
20 // 5. Shared code headers
21 #include <xmscore/stl/vector.h>
22 #include <xmscore/misc/Observer.h>
24 
25 // 6. Non-shared code headers
26 
27 //----- Forward declarations ---------------------------------------------------
28 
29 //----- External globals -------------------------------------------------------
30 
31 //----- Namespace declaration --------------------------------------------------
32 namespace xms
33 {
34 //----- Constants / Enumerations -----------------------------------------------
35 
36 //----- Classes / Structs ------------------------------------------------------
38 class ThreadMgrImpl : public ThreadMgr
39 {
40 public:
42  struct myThread
43  {
46  myThread(BSHP<ThreadLoop> a_)
47  : m_(a_)
48  {
49  }
50 
51  void operator()();
52  BSHP<ThreadLoop> m_;
53  };
54 
57  : m_prog()
58  , m_nThread(-1)
59  , m_nIter(-1)
60  {
61  }
62 
64  void SetThreadLoopClass(BSHP<ThreadLoop> a_) override { m_threader = a_; }
66  void ExplicitlySetNumThreads(int a_nThreads) override { m_nThread = a_nThreads; }
67  void RunThreads(int a_nIter) override;
69  void SetObserver(BSHP<Observer> a_prog) override { m_prog = a_prog; }
70  std::vector<BSHP<ThreadLoop>> Threads() override;
71 
72  void SetupThreads();
73  void MonitorThreads();
74  void FinishThreads();
75 
76  BSHP<ThreadLoop> m_threader;
77  BSHP<Observer> m_prog;
78  int m_nThread;
79  int m_nIter;
83  std::vector<myThread> m_vMyThread;
84  std::vector<boost::thread> m_vThreads;
85 };
86 //----- Internal functions -----------------------------------------------------
87 
88 //----- Class / Function definitions -------------------------------------------
89 
94 //------------------------------------------------------------------------------
96 //------------------------------------------------------------------------------
98 {
99  m_->DoWork();
100 } // InterpIdwImpl::myThread::operator
101 //------------------------------------------------------------------------------
104 //------------------------------------------------------------------------------
105 BSHP<ThreadMgr> ThreadMgr::New()
106 {
107  BSHP<ThreadMgr> ptr(new ThreadMgrImpl());
108  return ptr;
109 } // ThreadMgr::New
110 //------------------------------------------------------------------------------
112 //------------------------------------------------------------------------------
114 {
115 } // ThreadMgr::ThreadMgr
116 //------------------------------------------------------------------------------
118 //------------------------------------------------------------------------------
119 ThreadMgr::~ThreadMgr()
120 {
121 } // ThreadMgr::~ThreadMgr
122  //------------------------------------------------------------------------------
125  //------------------------------------------------------------------------------
126 std::vector<BSHP<ThreadLoop>> ThreadMgrImpl::Threads()
127 {
128  std::vector<BSHP<ThreadLoop>> threads;
129  for (size_t i = 0; i < m_vMyThread.size(); ++i)
130  {
131  threads.push_back(m_vMyThread[i].m_);
132  }
133  return threads;
134 } // ThreadMgrImpl::Threads
135 //------------------------------------------------------------------------------
138 //------------------------------------------------------------------------------
139 void ThreadMgrImpl::RunThreads(int a_nIter)
140 {
141  m_nIter = a_nIter;
142  if (m_nThread < 1)
143  m_nThread = boost::thread::hardware_concurrency() - 1;
144  if (m_nThread < 1)
145  m_nThread = 1;
146  SetupThreads();
147  MonitorThreads();
148  FinishThreads();
149 } // ThreadMgrImpl::RunThreads
150 //------------------------------------------------------------------------------
152 //------------------------------------------------------------------------------
154 {
155  m_beg.assign(m_nThread, 0);
156  m_counts.assign(m_nThread, 0);
157 
158  int cnt = (m_nIter / m_nThread) + 1;
159  m_thrdNumIter.assign(m_nThread, cnt);
160  for (int i = 1; i < m_nThread; ++i)
161  m_beg[i] = m_beg[i - 1] + cnt;
162  m_thrdNumIter.back() = m_nIter - m_beg.back();
163 
164  for (int i = 0; i < m_nThread; ++i)
165  {
166  BSHP<ThreadLoop> ptr = m_threader->CreateForNewThread();
167  ptr->SetStartNumIterCnt(m_beg[i], m_thrdNumIter[i], &m_counts[i]);
168  m_vMyThread.push_back(myThread(ptr));
169  }
170  // create the threads
171  for (int i = 0; i < m_nThread; ++i)
172  {
173  m_vThreads.push_back(boost::thread(m_vMyThread[i]));
174  }
175 } // ThreadMgrImpl::SetupThreads
176 //------------------------------------------------------------------------------
178 //------------------------------------------------------------------------------
180 {
181  double p(0.01);
182  if (m_prog)
183  m_prog->ProgressStatus(p);
184 
185  bool done(false);
186  int sum, cnt, cntBase;
187  cntBase = cnt = m_nIter / 100;
188  while (!done)
189  {
190  sum = 0;
191  for (size_t i = 0; i < m_counts.size(); ++i)
192  sum += m_counts[i];
193  while (sum >= cnt && p < 1)
194  {
195  p += .01;
196  if (m_prog)
197  m_prog->ProgressStatus(p);
198  cnt += cntBase;
199  }
200  boost::this_thread::sleep(boost::posix_time::millisec(100));
201  if (p >= 1)
202  done = true;
203  }
204 } // ThreadMgrImpl::MonitorThreads
205 //------------------------------------------------------------------------------
207 //------------------------------------------------------------------------------
209 {
210  for (size_t i = 0; i < m_vThreads.size(); ++i)
211  m_vThreads[i].join();
212 } // ThreadMgrImpl::FinishThreads
213 
214 } // namespace xms
void FinishThreads()
When all iterations are complete this terminates the threads.
Definition: ThreadMgr.cpp:208
std::vector< int > VecInt
std::vector< myThread > m_vMyThread
vector of created threads
Definition: ThreadMgr.cpp:83
void MonitorThreads()
Monitors the progress of all the threads.
Definition: ThreadMgr.cpp:179
Implmentation of ThreadMgr.
Definition: ThreadMgr.cpp:38
void SetThreadLoopClass(BSHP< ThreadLoop > a_) override
Definition: ThreadMgr.cpp:64
std::vector< BSHP< ThreadLoop > > Threads() override
Gets the threads managed by this class.
Definition: ThreadMgr.cpp:126
ThreadMgrImpl()
Constructor.
Definition: ThreadMgr.cpp:56
BSHP< Observer > m_prog
observes the threads and gives feedback of % complete
Definition: ThreadMgr.cpp:77
int m_nIter
total number of iterations for all threads combined
Definition: ThreadMgr.cpp:79
BSHP< ThreadLoop > m_threader
the worker class that is duplicated to run in multiple threads
Definition: ThreadMgr.cpp:76
void ExplicitlySetNumThreads(int a_nThreads) override
Definition: ThreadMgr.cpp:66
Manages a multi-threaded process.
Definition: ThreadMgr.h:27
static BSHP< ThreadMgr > New()
Creates a ThreadMgr.
Definition: ThreadMgr.cpp:105
ThreadMgr()
constructor
Definition: ThreadMgr.cpp:113
void RunThreads(int a_nIter) override
Executes the threads.
Definition: ThreadMgr.cpp:139
convenience struct for threads
Definition: ThreadMgr.cpp:42
int m_nThread
number of threads running
Definition: ThreadMgr.cpp:78
VecInt m_beg
starting index for each thread
Definition: ThreadMgr.cpp:80
std::vector< boost::thread > m_vThreads
actual boost threads that are run
Definition: ThreadMgr.cpp:84
VecInt m_thrdNumIter
number of iterations for each thread
Definition: ThreadMgr.cpp:81
myThread(BSHP< ThreadLoop > a_)
Constructor.
Definition: ThreadMgr.cpp:46
VecInt m_counts
number of completed iterations for each thread
Definition: ThreadMgr.cpp:82
void operator()()
worker code that is executed
Definition: ThreadMgr.cpp:97
BSHP< ThreadLoop > m_
Worker thread.
Definition: ThreadMgr.cpp:52
void SetupThreads()
Sets up the threads.
Definition: ThreadMgr.cpp:153
void SetObserver(BSHP< Observer > a_prog) override
Definition: ThreadMgr.cpp:69