diff Core/MultiThreading/RunnableWorkersPool.cpp @ 1681:ee4367497d0d

got rid of buggy BagOfRunnablesBySteps
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 06 Oct 2015 14:02:39 +0200
parents 15acdb19d520
children 559956d5ceb2
line wrap: on
line diff
--- a/Core/MultiThreading/RunnableWorkersPool.cpp	Tue Oct 06 13:36:09 2015 +0200
+++ b/Core/MultiThreading/RunnableWorkersPool.cpp	Tue Oct 06 14:02:39 2015 +0200
@@ -119,31 +119,39 @@
   }
 
 
-  RunnableWorkersPool::~RunnableWorkersPool()
+  void RunnableWorkersPool::Stop()
   {
-    pimpl_->continue_ = false;
-
-    for (size_t i = 0; i < pimpl_->workers_.size(); i++)
+    if (pimpl_->continue_)
     {
-      PImpl::Worker* worker = pimpl_->workers_[i];
+      pimpl_->continue_ = false;
+
+      for (size_t i = 0; i < pimpl_->workers_.size(); i++)
+      {
+        PImpl::Worker* worker = pimpl_->workers_[i];
 
-      if (worker != NULL)
-      {
-        worker->Join();
-        delete worker;
+        if (worker != NULL)
+        {
+          worker->Join();
+          delete worker;
+        }
       }
     }
   }
 
 
-  void RunnableWorkersPool::Add(IRunnableBySteps* runnable)
+  RunnableWorkersPool::~RunnableWorkersPool()
   {
-    pimpl_->queue_.Enqueue(runnable);
+    Stop();
   }
 
 
-  void RunnableWorkersPool::WaitDone()
+  void RunnableWorkersPool::Add(IRunnableBySteps* runnable)
   {
-    pimpl_->queue_.WaitEmpty(0);
+    if (!pimpl_->continue_)
+    {
+      throw OrthancException(ErrorCode_BadSequenceOfCalls);
+    }
+
+    pimpl_->queue_.Enqueue(runnable);
   }
 }