Template Class concurrent_queue
Defined in File concurrent_queue.h
Nested Relationships
Nested Types
Class Documentation
-
template<typename valueT>
class concurrent_queue A simple concurrent queue implementation; lifted with small changes from Herb Sutter’s “Writing a Generalized Concurrent Queue” http://drdobbs.com/high-performance-computing/211601363
The queue implementation uses a producer spinlock and a consumer spinlock, on the assumption that we’ll have multiple producers and consumers. Technically, we only have 1:N and N:1 situations, i.e. one producer and multiple consumers or vice versa. In the interest of simplicity, we’ll just stick with the N:M implementation Sutter provides.
The main change (other than some symbol name changes) is the addition of the size() and empty() functions, which use the consumer lock and can therefore contend with the consumers.
Note that while this implementation uses STL-ish symbol names, it makes no attempt at providing a full STL-like container.
Public Functions
-
inline concurrent_queue()
Constructor/destructor
-
inline ~concurrent_queue()
-
inline void push(valueT const &value)
Add new values to the queue with push() and remove them with pop(). The latter returns true if a value could be returned, false otherwise.
Multiple producers using push() contend for a producer lock. Multiple consumers using pop() contend for a consumer lock.
Producers and consumers do not contend with each other, however.
- Parameters
value – [in] The value to push into the queue.
-
template<typename iterT>
inline void push_range(iterT const &begin, iterT const &end) Push an entire range into the queue, defined by a beginning and end iterator. Note that the iterator is expected to have a single value, i.e. it is de-referenced with the * operator.
- Parameters
begin – [in] The start of the range.
end – [in] One past the last element of the range, as per standard C++ usage.
-
inline bool pop(valueT &result)
Pop a value from the queue.
- Parameters
result – [out] The location where the popped value will be stored.
- Returns
true if there was a value to pop, false otherwise - in which case the value of
result
is undefined.
-
inline std::tuple<bool, valueT> pop()
Variant of pop that returns a tuple of values.
- Returns
Tuple containing:
A boolean flag whether a value was present.
The retrieved value if the first parameter is true, otherwise a default constructed value.
-
inline bool empty() const
STL-ish information functions on the state of the queue. Both take the consumer’s point of view and contend for the consumer lock with pop().
Note that empty() is O(1), size() is O(N).
It is not advisable to use empty() or size() for testing whether or not pop() can be used.
-
inline concurrent_queue()