From a26a155f0c1bba56ada748c29f6a6841b794a7c9 Mon Sep 17 00:00:00 2001 From: hsc Date: Tue, 20 Nov 2012 15:01:55 +0000 Subject: [PATCH] SynchronizedQueue: optional upper bound git-svn-id: https://www4.informatik.uni-erlangen.de/i4svn/danceos/trunk/devel/fail@1944 8c4709b5-6ec9-48aa-a5cd-a96041d1645a --- src/core/util/SynchronizedQueue.hpp | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/core/util/SynchronizedQueue.hpp b/src/core/util/SynchronizedQueue.hpp index 03ae6929..cc72dcdf 100644 --- a/src/core/util/SynchronizedQueue.hpp +++ b/src/core/util/SynchronizedQueue.hpp @@ -17,11 +17,15 @@ template class SynchronizedQueue { // Adapted from: http://www.quantnet.com/cplusplus-multithreading-boost/ private: std::queue m_queue; //!< Use STL queue to store data + unsigned capacity; #ifndef __puma boost::mutex m_mutex; //!< The mutex to synchronise on boost::condition_variable m_cond; //!< The condition to wait for + boost::condition_variable m_cond_capacity; //!< Another condition to wait for #endif public: + SynchronizedQueue() : capacity(0) {} + SynchronizedQueue(unsigned capacity) : capacity(capacity) {} int Size() { #ifndef __puma @@ -36,6 +40,13 @@ public: #ifndef __puma boost::unique_lock lock(m_mutex); #endif + + while (capacity != 0 && m_queue.size() >= capacity) { +#ifndef __puma + m_cond_capacity.wait(lock); +#endif + } + // Add the data to the queue m_queue.push(data); // Notify others that data is ready @@ -62,6 +73,13 @@ public: #endif // Retrieve the data from the queue T result=m_queue.front(); m_queue.pop(); + + // Notify others that we have free slots +#ifndef __puma + if (m_queue.size() < capacity) { + m_cond_capacity.notify_one(); + } +#endif return result; } // Lock is automatically released here @@ -84,7 +102,13 @@ public: if (m_queue.size() > 0) { // Retrieve the data from the queue d = m_queue.front(); m_queue.pop(); - return true; + // Notify others that we have free slots +#ifndef __puma + if (m_queue.size() < capacity) { + m_cond_capacity.notify_one(); + } +#endif + return true; } else { return false; }