SynchronizedQueue: optional upper bound
git-svn-id: https://www4.informatik.uni-erlangen.de/i4svn/danceos/trunk/devel/fail@1944 8c4709b5-6ec9-48aa-a5cd-a96041d1645a
This commit is contained in:
@ -17,11 +17,15 @@ template <typename T>
|
|||||||
class SynchronizedQueue { // Adapted from: http://www.quantnet.com/cplusplus-multithreading-boost/
|
class SynchronizedQueue { // Adapted from: http://www.quantnet.com/cplusplus-multithreading-boost/
|
||||||
private:
|
private:
|
||||||
std::queue<T> m_queue; //!< Use STL queue to store data
|
std::queue<T> m_queue; //!< Use STL queue to store data
|
||||||
|
unsigned capacity;
|
||||||
#ifndef __puma
|
#ifndef __puma
|
||||||
boost::mutex m_mutex; //!< The mutex to synchronise on
|
boost::mutex m_mutex; //!< The mutex to synchronise on
|
||||||
boost::condition_variable m_cond; //!< The condition to wait for
|
boost::condition_variable m_cond; //!< The condition to wait for
|
||||||
|
boost::condition_variable m_cond_capacity; //!< Another condition to wait for
|
||||||
#endif
|
#endif
|
||||||
public:
|
public:
|
||||||
|
SynchronizedQueue() : capacity(0) {}
|
||||||
|
SynchronizedQueue(unsigned capacity) : capacity(capacity) {}
|
||||||
int Size()
|
int Size()
|
||||||
{
|
{
|
||||||
#ifndef __puma
|
#ifndef __puma
|
||||||
@ -36,6 +40,13 @@ public:
|
|||||||
#ifndef __puma
|
#ifndef __puma
|
||||||
boost::unique_lock<boost::mutex> lock(m_mutex);
|
boost::unique_lock<boost::mutex> lock(m_mutex);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
while (capacity != 0 && m_queue.size() >= capacity) {
|
||||||
|
#ifndef __puma
|
||||||
|
m_cond_capacity.wait(lock);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
// Add the data to the queue
|
// Add the data to the queue
|
||||||
m_queue.push(data);
|
m_queue.push(data);
|
||||||
// Notify others that data is ready
|
// Notify others that data is ready
|
||||||
@ -62,6 +73,13 @@ public:
|
|||||||
#endif
|
#endif
|
||||||
// Retrieve the data from the queue
|
// Retrieve the data from the queue
|
||||||
T result=m_queue.front(); m_queue.pop();
|
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;
|
return result;
|
||||||
} // Lock is automatically released here
|
} // Lock is automatically released here
|
||||||
|
|
||||||
@ -84,7 +102,13 @@ public:
|
|||||||
if (m_queue.size() > 0) {
|
if (m_queue.size() > 0) {
|
||||||
// Retrieve the data from the queue
|
// Retrieve the data from the queue
|
||||||
d = m_queue.front(); m_queue.pop();
|
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 {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user