Mercurial > hg > orthanc
comparison UnitTestsSources/MultiThreadingTests.cpp @ 1679:15acdb19d520
RunnableWorkersPool
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 06 Oct 2015 13:31:40 +0200 |
parents | 9ea3d082b064 |
children | ee4367497d0d |
comparison
equal
deleted
inserted
replaced
1666:d7039be97eeb | 1679:15acdb19d520 |
---|---|
37 #include "../Core/OrthancException.h" | 37 #include "../Core/OrthancException.h" |
38 #include "../Core/Toolbox.h" | 38 #include "../Core/Toolbox.h" |
39 #include "../Core/MultiThreading/Locker.h" | 39 #include "../Core/MultiThreading/Locker.h" |
40 #include "../Core/MultiThreading/Mutex.h" | 40 #include "../Core/MultiThreading/Mutex.h" |
41 #include "../Core/MultiThreading/ReaderWriterLock.h" | 41 #include "../Core/MultiThreading/ReaderWriterLock.h" |
42 #include "../Core/MultiThreading/RunnableWorkersPool.h" | |
42 | 43 |
43 using namespace Orthanc; | 44 using namespace Orthanc; |
44 | 45 |
45 namespace | 46 namespace |
46 { | 47 { |
181 //if (a == 84) { printf("BREAK\n"); return false; } | 182 //if (a == 84) { printf("BREAK\n"); return false; } |
182 | 183 |
183 outputs.push_back(boost::lexical_cast<std::string>(b)); | 184 outputs.push_back(boost::lexical_cast<std::string>(b)); |
184 } | 185 } |
185 | 186 |
186 Toolbox::USleep(100000); | 187 Toolbox::USleep(30000); |
187 | 188 |
188 return true; | 189 return true; |
189 } | 190 } |
190 }; | 191 }; |
191 | 192 |
200 s->GetListOfJobs(l); | 201 s->GetListOfJobs(l); |
201 for (ListOfStrings::iterator it = l.begin(); it != l.end(); ++it) | 202 for (ListOfStrings::iterator it = l.begin(); it != l.end(); ++it) |
202 { | 203 { |
203 printf(">> %s: %0.1f\n", it->c_str(), 100.0f * s->GetProgress(*it)); | 204 printf(">> %s: %0.1f\n", it->c_str(), 100.0f * s->GetProgress(*it)); |
204 } | 205 } |
205 Toolbox::USleep(10000); | 206 Toolbox::USleep(3000); |
206 } | 207 } |
207 } | 208 } |
208 | 209 |
209 | 210 |
210 TEST(MultiThreading, ServerScheduler) | 211 TEST(MultiThreading, ServerScheduler) |
255 if (t.joinable()) | 256 if (t.joinable()) |
256 { | 257 { |
257 t.join(); | 258 t.join(); |
258 } | 259 } |
259 } | 260 } |
261 | |
262 | |
263 namespace | |
264 { | |
265 class MyRunnable : public IRunnableBySteps | |
266 { | |
267 private: | |
268 unsigned int& output_; | |
269 unsigned int count_; | |
270 | |
271 public: | |
272 MyRunnable(unsigned int& output) : output_(output), count_(0) | |
273 { | |
274 } | |
275 | |
276 virtual bool Step() | |
277 { | |
278 count_ ++; | |
279 output_ ++; | |
280 | |
281 boost::this_thread::sleep(boost::posix_time::milliseconds(3)); | |
282 | |
283 return (count_ < 7); | |
284 } | |
285 }; | |
286 } | |
287 | |
288 | |
289 TEST(MultiThreading, RunnableWorkersPool) | |
290 { | |
291 unsigned int output = 0; | |
292 | |
293 { | |
294 RunnableWorkersPool pool(3); | |
295 | |
296 for (size_t i = 0; i < 11; i++) | |
297 { | |
298 pool.Add(new MyRunnable(output)); | |
299 } | |
300 | |
301 pool.WaitDone(); | |
302 } | |
303 | |
304 | |
305 ASSERT_EQ(11 * 7, output); | |
306 } |