a bounded queue of many producers and many consumers
More...
#include <ADS/concurrent/MPMCQueue.h>
|
| using | Item = T |
| | the type of the items
|
|
| | MPMCQueue (std::size_t capacity) |
| | the empty queue of capacity \( c \)
|
| | MPMCQueue (const MPMCQueue &)=delete |
| | no copy, since the threads share it by its address
|
| MPMCQueue & | operator= (const MPMCQueue &)=delete |
| | no copy, since the threads share it by its address
|
| | ~MPMCQueue () |
| | destroys the items left
|
| template<class... Args> |
| bool | emplace (Args &&... args) |
| | enqueues the item built from args, false if the queue is full (and then nothing is built); any thread may call it
|
| bool | push (const T &v) |
| | enqueues a copy of \( v \), false if the queue is full; any thread may call it
|
| bool | push (T &&v) |
| | enqueues \( v \), moved, false if the queue is full; any thread may call it
|
| bool | tryPop (T &x) |
| | dequeues an item into \( x \), false if the queue is empty; any thread may call it
|
| std::size_t | size (void) const |
| | the number of items at some moment of the call, at most \( c \)
|
| bool | empty (void) const |
| | true if the queue held no item at some moment of the call
|
| std::size_t | capacity (void) const |
| | the capacity \( c \)
|
template<class T>
class ADS::MPMCQueue< T >
a bounded queue of many producers and many consumers
◆ Item
◆ MPMCQueue() [1/2]
the empty queue of capacity \( c \)
- Precondition
- \( c \ge 2 \) is a power of 2
◆ MPMCQueue() [2/2]
no copy, since the threads share it by its address
◆ ~MPMCQueue()
◆ capacity()
◆ emplace()
template<class T>
template<class... Args>
enqueues the item built from args, false if the queue is full (and then nothing is built); any thread may call it
In pseudocode (the queue of Vyukov, beyond the book), \( C[ i ] \) the place \( i \bmod c \) with its sequence \( C[ i ].s \) and \( t \) the tail:
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{function}\ \mathrm{emplace}( x ) \cr
2 & \quad p \gets t \cr
3 & \quad \textbf{loop} \cr
4 & \qquad s \gets C[ p ].s\quad \triangleright\ \text{acquire} \cr
5 & \qquad \textbf{if}\ s = p\ \textbf{then}\quad \triangleright
\ \text{the place is free for } p \cr
6 & \qquad\quad \textbf{if}\ \mathrm{CAS}( t , p , p + 1 )
\ \textbf{then} \cr
7 & \qquad\qquad C[ p ].x \gets x;\ C[ p ].s \gets p + 1\quad
\triangleright\ \text{release} \cr
8 & \qquad\qquad \textbf{return true} \cr
9 & \qquad\quad \textbf{else}\ p \gets t\quad \triangleright
\ \text{the CAS failed and read it} \cr
10 & \qquad \textbf{else if}\ s < p\ \textbf{then return false}\quad
\triangleright\ \text{full} \cr
11 & \qquad \textbf{else}\ p \gets t\quad \triangleright\ \text{another}
\text{ producer took } p
\end{array}}
\]
◆ empty()
true if the queue held no item at some moment of the call
◆ operator=()
no copy, since the threads share it by its address
◆ push() [1/2]
enqueues a copy of \( v \), false if the queue is full; any thread may call it
◆ push() [2/2]
enqueues \( v \), moved, false if the queue is full; any thread may call it
◆ size()
the number of items at some moment of the call, at most \( c \)
◆ tryPop()
dequeues an item into \( x \), false if the queue is empty; any thread may call it
In pseudocode (the queue of Vyukov, beyond the book), \( h \) the head:
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{function}\ \mathrm{tryPop}( x ) \cr
2 & \quad q \gets h \cr
3 & \quad \textbf{loop} \cr
4 & \qquad s \gets C[ q ].s\quad \triangleright\ \text{acquire} \cr
5 & \qquad \textbf{if}\ s = q + 1\ \textbf{then}\quad \triangleright
\ \text{the item of } q \text{ is there} \cr
6 & \qquad\quad \textbf{if}\ \mathrm{CAS}( h , q , q + 1 )
\ \textbf{then} \cr
7 & \qquad\qquad x \gets C[ q ].x;\ C[ q ].s \gets q + c\quad
\triangleright\ \text{release} \cr
8 & \qquad\qquad \textbf{return true} \cr
9 & \qquad\quad \textbf{else}\ q \gets h \cr
10 & \qquad \textbf{else if}\ s < q + 1\ \textbf{then return false}\quad
\triangleright\ \text{empty} \cr
11 & \qquad \textbf{else}\ q \gets h
\end{array}}
\]
The documentation for this class was generated from the following file: