diff 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
line wrap: on
line diff
--- a/UnitTestsSources/MultiThreadingTests.cpp	Wed Sep 30 14:04:25 2015 +0200
+++ b/UnitTestsSources/MultiThreadingTests.cpp	Tue Oct 06 13:31:40 2015 +0200
@@ -39,6 +39,7 @@
 #include "../Core/MultiThreading/Locker.h"
 #include "../Core/MultiThreading/Mutex.h"
 #include "../Core/MultiThreading/ReaderWriterLock.h"
+#include "../Core/MultiThreading/RunnableWorkersPool.h"
 
 using namespace Orthanc;
 
@@ -183,7 +184,7 @@
       outputs.push_back(boost::lexical_cast<std::string>(b));
     }
 
-    Toolbox::USleep(100000);
+    Toolbox::USleep(30000);
 
     return true;
   }
@@ -202,7 +203,7 @@
     {
       printf(">> %s: %0.1f\n", it->c_str(), 100.0f * s->GetProgress(*it));
     }
-    Toolbox::USleep(10000);
+    Toolbox::USleep(3000);
   }
 }
 
@@ -257,3 +258,49 @@
     t.join();
   }
 }
+
+
+namespace
+{
+  class MyRunnable : public IRunnableBySteps
+  {
+  private:
+    unsigned int& output_;
+    unsigned int count_;
+
+  public:
+    MyRunnable(unsigned int& output) : output_(output), count_(0)
+    {
+    }
+
+    virtual bool Step()
+    {
+      count_ ++;
+      output_ ++;
+
+      boost::this_thread::sleep(boost::posix_time::milliseconds(3));
+
+      return (count_ < 7);
+    }
+  };
+}
+
+
+TEST(MultiThreading, RunnableWorkersPool)
+{
+  unsigned int output = 0;
+
+  {
+    RunnableWorkersPool pool(3);
+  
+    for (size_t i = 0; i < 11; i++)
+    {
+      pool.Add(new MyRunnable(output));
+    }
+
+    pool.WaitDone();
+  }
+
+
+  ASSERT_EQ(11 * 7, output);
+}