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

an inbox filled by many threads and emptied by one, lock-free More...

#include <ADS/concurrent/MPSCInbox.h>

Public types

using Item = T
 the type of the messages

Public member functions

 MPSCInbox (void)=default
 the empty inbox
 MPSCInbox (const MPSCInbox &)=delete
 no copy, since the threads share it by its address
MPSCInbox & operator= (const MPSCInbox &)=delete
 no copy, since the threads share it by its address
 ~MPSCInbox ()
 frees the messages not taken
void push (T v)
 sends \( v \), and wakes the consumer if it sleeps in waitTake() and the stack was empty (a fence and the read of a line when it does not); any thread may call it
std::vector< T > take (void)
 all the messages sent so far and not yet taken, those of each sender in the order of sending; only the consumer calls it
std::size_t takeInto (std::vector< T > &out)
 the messages of take() appended to out, which keeps its room from call to call, and their number; only the consumer calls it
std::vector< T > waitTake (void)
 the messages of take(), sleeping until at least one comes [see EventCount.h]; only the consumer calls it
bool empty (void) const
 true if no message was waiting at some moment of the call

Detailed description

template<class T>
class ADS::MPSCInbox< T >

an inbox filled by many threads and emptied by one, lock-free

Member typedef documentation

◆ Item

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

the type of the messages

Constructor and destructor documentation

◆ MPSCInbox() [1/2]

template<class T>
ADS::MPSCInbox< T >::MPSCInbox ( void )
default

the empty inbox

◆ MPSCInbox() [2/2]

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

no copy, since the threads share it by its address

◆ ~MPSCInbox()

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

frees the messages not taken

Member function documentation

◆ empty()

template<class T>
bool ADS::MPSCInbox< T >::empty ( void ) const
nodiscard

true if no message was waiting at some moment of the call

◆ operator=()

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

no copy, since the threads share it by its address

◆ push()

template<class T>
void ADS::MPSCInbox< T >::push ( T v)

sends \( v \), and wakes the consumer if it sleeps in waitTake() and the stack was empty (a fence and the read of a line when it does not); any thread may call it

In pseudocode (the push of Treiber, beyond the book), with \( \mathit{top} \) the top of the stack:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{push}( v ) \cr 2 & \quad c \gets \text{the next cell of the block of the thread,} \text{ holding } v \cr 3 & \quad \textbf{repeat}\ c.\mathit{next} \gets \mathit{top} \ \textbf{until}\ \mathrm{CAS}( \mathit{top} , c.\mathit{next} , c )\quad \triangleright\ \text{release} \cr 4 & \quad \textbf{if}\ c.\mathit{next} = \textbf{nil}\ \textbf{then} \ \mathrm{notify}() \end{array}} \]

◆ take()

template<class T>
std::vector< T > ADS::MPSCInbox< T >::take ( void )
nodiscard

all the messages sent so far and not yet taken, those of each sender in the order of sending; only the consumer calls it

In pseudocode (beyond the book), the whole stack taken at once:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{take}() \cr 2 & \quad c \gets \mathrm{exchange}( \mathit{top} , \textbf{nil} )\quad \triangleright\ \text{acquire} \cr 3 & \quad L \gets \text{the values of the cells from } c \text{, newest} \text{ first} \cr 4 & \quad \text{add to the count of each block the cells taken from} \text{ it};\ \text{give back to its owner a block whose count} \text{ is } 64 \cr 5 & \quad \textbf{return}\ L \text{ reversed} \end{array}} \]

◆ takeInto()

template<class T>
std::size_t ADS::MPSCInbox< T >::takeInto ( std::vector< T > & out)

the messages of take() appended to out, which keeps its room from call to call, and their number; only the consumer calls it

◆ waitTake()

template<class T>
std::vector< T > ADS::MPSCInbox< T >::waitTake ( void )
inlinenodiscard

the messages of take(), sleeping until at least one comes [see EventCount.h]; only the consumer calls it


The documentation for this class was generated from the following file: