|
ADS
Algorithms and Data Structures
|
an unbounded queue from many producers to one consumer, a lane each More...
#include <ADS/concurrent/MPSCQueue.h>
Classes | |
| class | Producer |
| the handle of a producer, its lane: one thread at a time pushes More... | |
Public types | |
| using | Item = T |
| the type of the items | |
Public member functions | |
| MPSCQueue (void)=default | |
| the queue with no lane | |
| MPSCQueue (const MPSCQueue &)=delete | |
| no copy, since the producers hold its lanes | |
| MPSCQueue & | operator= (const MPSCQueue &)=delete |
| no copy, since the producers hold its lanes | |
| ~MPSCQueue () | |
| destroys the items left, the blocks and the lanes | |
| Producer | producer (void) |
| a new producer, with a lane of its own; any thread may call it | |
| bool | tryPop (T &x) |
| dequeues an item into \( x \), false if none was found in a turn of the lanes; only the consumer calls it | |
| std::size_t | popInto (T *out, std::size_t n) |
| dequeues up to \( n \) items into out, those of a lane before the next one, and returns how many; only the consumer calls it | |
| void | waitPop (T &x) |
| dequeues an item into \( x \), sleeping until one comes [see EventCount.h], the producers calling notify() after their pushes; only the consumer calls it | |
| std::size_t | waitPopInto (T *out, std::size_t n) |
| the items of popInto(), sleeping until at least one comes | |
| bool | empty (void) |
| true if no item was found in a turn of the lanes; only the consumer calls it | |
an unbounded queue from many producers to one consumer, a lane each
| using ADS::MPSCQueue< T, B >::Item = T |
the type of the items
|
default |
the queue with no lane
|
delete |
no copy, since the producers hold its lanes
| ADS::MPSCQueue< T, B >::~MPSCQueue | ( | ) |
destroys the items left, the blocks and the lanes
|
nodiscard |
true if no item was found in a turn of the lanes; only the consumer calls it
|
delete |
no copy, since the producers hold its lanes
| std::size_t ADS::MPSCQueue< T, B >::popInto | ( | T * | out, |
| std::size_t | n ) |
dequeues up to \( n \) items into out, those of a lane before the next one, and returns how many; only the consumer calls it
In pseudocode (the list of bounded buffers of Aldinucci et al. and the wait for a batch of B-Queue, beyond the book), the lanes taken in turn from the one of the last call, \( b \) and \( h \) the block and the place of the consumer in the lane \( \ell \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{popInto}( \mathit{out} , n ) \cr 2 & \quad \textbf{foreach}\ \text{lane } \ell \text{ in turn, from the} \text{ last one drained}\ \textbf{do} \cr 3 & \qquad k \gets 0 \cr 4 & \qquad \textbf{while}\ k < n\ \textbf{do} \cr 5 & \qquad\quad \textbf{if}\ h = B\ \textbf{then} \cr 6 & \qquad\qquad \textbf{if}\ b.\mathit{next} = \textbf{nil} \ \textbf{then break}\quad \triangleright\ \text{acquire} \cr 7 & \qquad\qquad \text{push } b \text{ on the stack given back of } \ell \text{ by a CAS};\ b \gets b.\mathit{next};\ h \gets 0 \cr 8 & \qquad\quad f \gets b.f\quad \triangleright\ \text{acquire} \cr 9 & \qquad\quad \textbf{if}\ f - h < \min( B , 64 )\ \textbf{and}\ f < B\ \textbf{and}\ \ell.\mathit{streaming}\ \textbf{then} \cr 10 & \qquad\qquad \text{wait 64 pauses};\ f \gets b.f \cr 11 & \qquad\quad \textbf{if}\ h = \min( f , h + n - k ) \ \textbf{then break} \cr 12 & \qquad\quad \textbf{while}\ h < \min( f , h + n - k ) \ \textbf{do} \cr 13 & \qquad\qquad \mathit{out}[ k ] \gets b[ h ];\ h \gets h + 1;\ k \gets k + 1 \cr 14 & \qquad \ell.\mathit{streaming} \gets k > 0 \cr 15 & \qquad \textbf{if}\ k > 0\ \textbf{then return}\ k \cr 16 & \quad \textbf{return}\ 0 \end{array}} \]
|
nodiscard |
a new producer, with a lane of its own; any thread may call it
In pseudocode (the stack of Treiber, beyond the book), \( L \) the head of the list of the lanes:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{producer}() \cr 2 & \quad \ell \gets \text{a new lane with one empty block} \cr 3 & \quad \textbf{repeat}\ \ell.\mathit{next} \gets L\ \textbf{until} \ \mathrm{CAS}( L , \ell.\mathit{next} , \ell )\quad \triangleright \ \text{release} \cr 4 & \quad \textbf{return}\ \ell \end{array}} \]
| bool ADS::MPSCQueue< T, B >::tryPop | ( | T & | x | ) |
dequeues an item into \( x \), false if none was found in a turn of the lanes; only the consumer calls it
It is popInto() below, for one item.
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{tryPop}( x ) \cr 2 & \quad \textbf{return}\ \mathrm{popInto}( x , 1 ) = 1 \end{array}} \]
|
inline |
dequeues an item into \( x \), sleeping until one comes [see EventCount.h], the producers calling notify() after their pushes; only the consumer calls it
In pseudocode (beyond the book), on the wait of EventCount:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{waitPop}( x ) \cr 2 & \quad \mathrm{await}( \mathrm{tryPop}( x ) ) \end{array}} \]
|
inline |
the items of popInto(), sleeping until at least one comes