ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::MPMCQueue< T > Class Template Reference

a bounded queue of many producers and many consumers More...

#include <ADS/concurrent/MPMCQueue.h>

Public types

using Item = T
 the type of the items

Public member functions

 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 \)

Detailed description

template<class T>
class ADS::MPMCQueue< T >

a bounded queue of many producers and many consumers

Member typedef documentation

◆ Item

template<class T>
using ADS::MPMCQueue< T >::Item = T

the type of the items

Constructor and destructor documentation

◆ MPMCQueue() [1/2]

template<class T>
ADS::MPMCQueue< T >::MPMCQueue ( std::size_t capacity)
explicit

the empty queue of capacity \( c \)

Precondition
\( c \ge 2 \) is a power of 2

◆ MPMCQueue() [2/2]

template<class T>
ADS::MPMCQueue< T >::MPMCQueue ( const MPMCQueue< T > & )
delete

no copy, since the threads share it by its address

◆ ~MPMCQueue()

template<class T>
ADS::MPMCQueue< T >::~MPMCQueue ( )

destroys the items left

Member function documentation

◆ capacity()

template<class T>
std::size_t ADS::MPMCQueue< T >::capacity ( void ) const
inlinenodiscard

the capacity \( c \)

◆ emplace()

template<class T>
template<class... Args>
bool ADS::MPMCQueue< T >::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

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()

template<class T>
bool ADS::MPMCQueue< T >::empty ( void ) const
inlinenodiscard

true if the queue held no item at some moment of the call

◆ operator=()

template<class T>
MPMCQueue & ADS::MPMCQueue< T >::operator= ( const MPMCQueue< T > & )
delete

no copy, since the threads share it by its address

◆ push() [1/2]

template<class T>
bool ADS::MPMCQueue< T >::push ( const T & v)
inline

enqueues a copy of \( v \), false if the queue is full; any thread may call it

◆ push() [2/2]

template<class T>
bool ADS::MPMCQueue< T >::push ( T && v)
inline

enqueues \( v \), moved, false if the queue is full; any thread may call it

◆ size()

template<class T>
std::size_t ADS::MPMCQueue< T >::size ( void ) const
nodiscard

the number of items at some moment of the call, at most \( c \)

◆ tryPop()

template<class T>
bool ADS::MPMCQueue< T >::tryPop ( T & x)

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: