|
ADS
Algorithms and Data Structures
|
the deque of an owner that pushes and takes, and of thieves that steal More...
#include <ADS/concurrent/WorkStealingDeque.h>
Public types | |
| using | Item = T |
| the type of the items | |
Public member functions | |
| WorkStealingDeque (std::size_t capacity=1024) | |
| the empty deque on a vector of capacity \( c \) | |
| WorkStealingDeque (const WorkStealingDeque &)=delete | |
| no copy, since the thieves share it by its address | |
| WorkStealingDeque & | operator= (const WorkStealingDeque &)=delete |
| no copy, since the thieves share it by its address | |
| void | push (T x) |
| pushes \( x \) at the bottom, doubling the vector if it is full; only the owner calls it | |
| std::optional< T > | take (void) |
| takes the item at the bottom, the last pushed, nothing if the deque is empty or a thief took the last item first; only the owner calls it | |
| bool | steal (T &x) |
| steals the item at the top, the first pushed, into \( x \); false if the deque is empty or another thread took that item first (and then a thief may try again); any thread may call it | |
| std::size_t | size (void) const |
| the number of items at some moment of the call, \( b - t \), exact for the owner | |
| bool | empty (void) const |
| true if the deque held no item at some moment of the call | |
| std::size_t | capacity (void) const |
| the places of the current vector; only the owner calls it | |
the deque of an owner that pushes and takes, and of thieves that steal
| using ADS::WorkStealingDeque< T >::Item = T |
the type of the items
|
explicit |
the empty deque on a vector of capacity \( c \)
|
delete |
no copy, since the thieves share it by its address
|
inlinenodiscard |
the places of the current vector; only the owner calls it
|
inlinenodiscard |
true if the deque held no item at some moment of the call
|
delete |
no copy, since the thieves share it by its address
| void ADS::WorkStealingDeque< T >::push | ( | T | x | ) |
pushes \( x \) at the bottom, doubling the vector if it is full; only the owner calls it
In pseudocode (Chase and Lev, beyond the book), \( A \) the vector of \( |A| \) places, the index \( i \) at its place \( i \bmod |A| \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{push}( x ) \cr 2 & \quad b' \gets b;\ t' \gets t\quad \triangleright\ \text{relaxed} \text{ and acquire} \cr 3 & \quad \textbf{if}\ b' - t' = |A|\ \textbf{then} \cr 4 & \qquad A' \gets \text{a vector of } 2 |A| \text{ places}; \ \textbf{for}\ i \gets t'\ \textbf{to}\ b' - 1\ \textbf{do}\ A'[ i ] \gets A[ i ] \cr 5 & \qquad A \gets A'\quad \triangleright\ \text{release, the old} \text{ vector kept for the thieves} \cr 6 & \quad A[ b' ] \gets x \cr 7 & \quad b \gets b' + 1\quad \triangleright\ \text{release} \end{array}} \]
|
nodiscard |
the number of items at some moment of the call, \( b - t \), exact for the owner
| bool ADS::WorkStealingDeque< T >::steal | ( | T & | x | ) |
steals the item at the top, the first pushed, into \( x \); false if the deque is empty or another thread took that item first (and then a thief may try again); any thread may call it
In pseudocode (Chase and Lev with the orders of Lê et al., beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{steal}( x ) \cr 2 & \quad t' \gets t;\ b' \gets b\quad \triangleright\ \text{both} \text{ sequentially consistent, in this order} \cr 3 & \quad \textbf{if}\ t' \ge b'\ \textbf{then return false} \cr 4 & \quad y \gets A[ t' ]\quad \triangleright\ A \text{ read with} \text{ acquire} \cr 5 & \quad \textbf{if not}\ \mathrm{CAS}( t , t' , t' + 1 ) \ \textbf{then return false} \cr 6 & \quad x \gets y;\ \textbf{return true} \end{array}} \]
| std::optional< T > ADS::WorkStealingDeque< T >::take | ( | void | ) |
takes the item at the bottom, the last pushed, nothing if the deque is empty or a thief took the last item first; only the owner calls it
In pseudocode (Chase and Lev with the orders of Lê et al., beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{take}() \cr 2 & \quad b' \gets b - 1;\ b \gets b'\quad \triangleright \ \text{sequentially consistent, before the read of } t \cr 3 & \quad t' \gets t\quad \triangleright\ \text{sequentially} \text{ consistent} \cr 4 & \quad \textbf{if}\ t' > b'\ \textbf{then}\ b \gets b' + 1; \ \textbf{return nil}\quad \triangleright\ \text{empty} \cr 5 & \quad x \gets A[ b' ] \cr 6 & \quad \textbf{if}\ t' < b'\ \textbf{then return}\ x\quad \triangleright\ \text{no thief reaches } b' \cr 7 & \quad \mathit{won} \gets \mathrm{CAS}( t , t' , t' + 1 )\quad \triangleright\ \text{the last item, raced for} \cr 8 & \quad b \gets b' + 1 \cr 9 & \quad \textbf{if}\ \mathit{won}\ \textbf{then return}\ x \ \textbf{else return nil} \end{array}} \]