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

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

Detailed description

template<class T>
class ADS::WorkStealingDeque< T >

the deque of an owner that pushes and takes, and of thieves that steal

Member typedef documentation

◆ Item

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

the type of the items

Constructor and destructor documentation

◆ WorkStealingDeque() [1/2]

template<class T>
ADS::WorkStealingDeque< T >::WorkStealingDeque ( std::size_t capacity = 1024)
explicit

the empty deque on a vector of capacity \( c \)

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

◆ WorkStealingDeque() [2/2]

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

no copy, since the thieves share it by its address

Member function documentation

◆ capacity()

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

the places of the current vector; only the owner calls it

◆ empty()

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

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

◆ operator=()

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

no copy, since the thieves share it by its address

◆ push()

template<class T>
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}} \]

◆ size()

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

the number of items at some moment of the call, \( b - t \), exact for the owner

◆ steal()

template<class T>
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}} \]

◆ take()

template<class T>
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}} \]


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