|
ADS
Algorithms and Data Structures
|
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 | |
an inbox filled by many threads and emptied by one, lock-free
| using ADS::MPSCInbox< T >::Item = T |
the type of the messages
|
default |
the empty inbox
|
delete |
no copy, since the threads share it by its address
| ADS::MPSCInbox< T >::~MPSCInbox | ( | ) |
frees the messages not taken
|
nodiscard |
true if no message was waiting at some moment of the call
|
delete |
no copy, since the threads share it by its address
| 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}} \]
|
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}} \]
| 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
|
inlinenodiscard |
the messages of take(), sleeping until at least one comes [see EventCount.h]; only the consumer calls it