changeset 6532:30eb6295e559

Blocking queue: WaitEmpty
author Alain Mazy <am@orthanc.team>
date Thu, 04 Dec 2025 18:22:59 +0100
parents c4cfb25a932b
children ed9142ea378d
files OrthancFramework/Sources/MultiThreading/BlockingSharedMessageQueue.cpp OrthancFramework/Sources/MultiThreading/BlockingSharedMessageQueue.h
diffstat 2 files changed, 35 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancFramework/Sources/MultiThreading/BlockingSharedMessageQueue.cpp	Tue Dec 02 17:08:41 2025 +0100
+++ b/OrthancFramework/Sources/MultiThreading/BlockingSharedMessageQueue.cpp	Thu Dec 04 18:22:59 2025 +0100
@@ -103,9 +103,39 @@
 
     roomAvailable_.notify_one();
 
+    if (queue_.empty())
+    {
+      emptied_.notify_all();
+    }
+
     return message.release();
   }
 
+  bool BlockingSharedMessageQueue::WaitEmpty(int32_t millisecondsTimeout)
+  {
+    boost::mutex::scoped_lock lock(mutex_);
+    
+    // Wait for the queue to become empty
+    while (!queue_.empty())
+    {
+      if (millisecondsTimeout == 0)
+      {
+        emptied_.wait(lock);
+      }
+      else
+      {
+        if (!emptied_.timed_wait
+            (lock, boost::posix_time::milliseconds(millisecondsTimeout)))
+        {
+          return false;
+        }
+      }
+    }
+
+    return true;
+  }
+
+
   void BlockingSharedMessageQueue::Clear()
   {
     boost::mutex::scoped_lock lock(mutex_);
@@ -124,6 +154,8 @@
         roomAvailable_.notify_one();
       }
     }
+
+    emptied_.notify_all();
   }
 
   size_t BlockingSharedMessageQueue::GetSize()
--- a/OrthancFramework/Sources/MultiThreading/BlockingSharedMessageQueue.h	Tue Dec 02 17:08:41 2025 +0100
+++ b/OrthancFramework/Sources/MultiThreading/BlockingSharedMessageQueue.h	Thu Dec 04 18:22:59 2025 +0100
@@ -45,6 +45,7 @@
     boost::mutex mutex_;
     boost::condition_variable elementAvailable_;
     boost::condition_variable roomAvailable_;
+    boost::condition_variable emptied_;
 
   public:
     explicit BlockingSharedMessageQueue(unsigned int maxSize = 0);
@@ -60,6 +61,8 @@
     // The caller is responsible to delete the dequeued message!
     IDynamicObject* Dequeue(int32_t millisecondsTimeout);
 
+    bool WaitEmpty(int32_t millisecondsTimeout);
+
     void Clear();
 
     size_t GetSize();