summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMatthias P. Braendli <matthias.braendli@mpb.li>2014-02-21 10:55:36 +0100
committerMatthias P. Braendli <matthias.braendli@mpb.li>2014-02-21 10:55:36 +0100
commit1500cb3368e2522d98d13b51339e560159d3741b (patch)
treecb3f857679fb28a4de32155a151699e62e3a4b00 /src
parente8c274301aca0bca1b7d1e6086cf29af4645732e (diff)
downloaddabmod-1500cb3368e2522d98d13b51339e560159d3741b.tar.gz
dabmod-1500cb3368e2522d98d13b51339e560159d3741b.tar.bz2
dabmod-1500cb3368e2522d98d13b51339e560159d3741b.zip
Some ThreadsafeQueue comments
Diffstat (limited to 'src')
-rw-r--r--src/ThreadsafeQueue.h36
1 files changed, 28 insertions, 8 deletions
diff --git a/src/ThreadsafeQueue.h b/src/ThreadsafeQueue.h
index b5ee5b6..3dd5450 100644
--- a/src/ThreadsafeQueue.h
+++ b/src/ThreadsafeQueue.h
@@ -2,7 +2,7 @@
Copyright (C) 2007, 2008, 2009, 2010, 2011 Her Majesty the Queen in
Right of Canada (Communications Research Center Canada)
- Copyright (C) 2013
+ Copyright (C) 2013, 2014
Matthias P. Braendli, matthias.braendli@mpb.li
An implementation for a threadsafe queue using boost thread library
@@ -34,27 +34,41 @@
#include <boost/thread.hpp>
#include <queue>
+/* This queue is meant to be used by two threads. One producer
+ * that pushes elements into the queue, and one consumer that
+ * retrieves the elements.
+ *
+ * The queue can make the consumer block until enough elements
+ * are available.
+ */
+
template<typename T>
class ThreadsafeQueue
{
-private:
- std::queue<T> the_queue;
- mutable boost::mutex the_mutex;
- boost::condition_variable the_condition_variable;
- size_t the_required_size;
public:
-
+ /* Create a new queue without any minimum required
+ * fill before it is possible to pop an element
+ */
ThreadsafeQueue() : the_required_size(1) {}
+ /* Create a queue where it has to contain at least
+ * required_size elements before pop is possible
+ */
ThreadsafeQueue(size_t required_size) : the_required_size(required_size) {}
+ /* Push one element into the queue, and notify another thread that
+ * might be waiting.
+ *
+ * returns the new queue size.
+ */
size_t push(T const& val)
{
boost::mutex::scoped_lock lock(the_mutex);
the_queue.push(val);
size_t queue_size = the_queue.size();
lock.unlock();
- the_condition_variable.notify_one();
+
+ notify();
return queue_size;
}
@@ -94,6 +108,12 @@ public:
popped_value = the_queue.front();
the_queue.pop();
}
+
+private:
+ std::queue<T> the_queue;
+ mutable boost::mutex the_mutex;
+ boost::condition_variable the_condition_variable;
+ size_t the_required_size;
};
#endif