ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::ThreadPool Class Reference

a pool of threads with work stealing, an executor More...

#include <ADS/parallel/ThreadPool.h>

Public types

using lock_type = std::mutex
 the lock of the data shared by the workers of run()

Public member functions

 ThreadPool (std::size_t p=0, std::int64_t spin=2000000, std::int64_t maxSpin=200000000)
 a pool of \( p \) threads, the caller included (the number of the hardware threads if \( p = 0 \)), whose idle workers look for tasks for between spin and maxSpin ns, after the gaps they saw, before they sleep (maxSpin raised to spin if below it)
 ThreadPool (const ThreadPool &)=delete
 no copy, since the workers hold the address of the pool
ThreadPool & operator= (const ThreadPool &)=delete
 no copy, since the workers hold the address of the pool
 ~ThreadPool ()
 stops the workers, which first finish their tasks
std::size_t concurrency (void) const
 the number of threads, the caller included
template<class F>
void bulk (std::size_t n, F &&f)
 the calls \( f( 0 ) , \ldots , f( n - 1 ) \) on the threads of the pool, returning when all have returned
template<class F, class G>
void invoke (F &&f, G &&g)
 the calls f() and g() in parallel, returning when both have returned
template<class F>
void run (std::size_t p, F &&f)
 the calls \( f( 0 ) , \ldots , f( p - 1 ) \) of long-lived workers, each begun by a thread of the pool that holds no other one, and all at the same time on \( p \) distinct threads if none returns before all have begun; returns when all have returned

Detailed description

a pool of threads with work stealing, an executor

Member typedef documentation

◆ lock_type

using ADS::ThreadPool::lock_type = std::mutex

the lock of the data shared by the workers of run()

Constructor and destructor documentation

◆ ThreadPool() [1/2]

ADS::ThreadPool::ThreadPool ( std::size_t p = 0,
std::int64_t spin = 2000000,
std::int64_t maxSpin = 200000000 )
explicit

a pool of \( p \) threads, the caller included (the number of the hardware threads if \( p = 0 \)), whose idle workers look for tasks for between spin and maxSpin ns, after the gaps they saw, before they sleep (maxSpin raised to spin if below it)

Precondition
\( p \ge 1 \) once the hardware threads are counted

◆ ThreadPool() [2/2]

ADS::ThreadPool::ThreadPool ( const ThreadPool & )
delete

no copy, since the workers hold the address of the pool

◆ ~ThreadPool()

ADS::ThreadPool::~ThreadPool ( )

stops the workers, which first finish their tasks

Member function documentation

◆ bulk()

template<class F>
void ADS::ThreadPool::bulk ( std::size_t n,
F && f )

the calls \( f( 0 ) , \ldots , f( n - 1 ) \) on the threads of the pool, returning when all have returned

In pseudocode (a job of tokens on the deques of Chase and Lev, beyond the book), \( D_s \) the deque of the slot \( s \) of the caller:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{bulk}( n , f ) \cr 2 & \quad \textbf{if}\ p = 1\ \textbf{or}\ n = 1\ \textbf{then} \ \textbf{for}\ i \gets 0\ \textbf{to}\ n - 1\ \textbf{do}\ f( i ); \ \textbf{return} \cr 3 & \quad J \gets \text{a job on the stack};\ J.\mathit{next} \gets 0; \ h \gets \min\{ p - 1 , n - 1 \};\ J.\mathit{out} \gets h \cr 4 & \quad \text{push } h \text{ tokens of } J \text{ on } D_s; \ \text{wake the threads that sleep} \cr 5 & \quad \mathrm{work}( J ) \cr 6 & \quad \textbf{while}\ \text{the bottom of } D_s \text{ is a token } \text{of } J\ \textbf{do} \cr 7 & \qquad \mathrm{take}( D_s );\ J.\mathit{out} \gets J.\mathit{out} - 1 \cr 8 & \quad \textbf{while}\ J.\mathit{out} \ne 0\ \textbf{do}\ \text{steal } \text{a task and run it, or pause} \cr 9 & \quad \text{throw again the first exception of the calls, if any} \cr 10 & \textbf{procedure}\ \mathrm{work}( J )\quad \triangleright \ \text{also run by a thief of a token, which then decrements } J.\mathit{out} \cr 11 & \quad \textbf{loop} \cr 12 & \qquad i \gets \mathrm{fetchAndAdd}( J.\mathit{next} , 1 ); \ \textbf{if}\ i \ge n\ \textbf{then return} \cr 13 & \qquad f( i )\quad \triangleright\ \text{if it throws, keep the } \text{exception and set } J.\mathit{next} \gets n \end{array}} \]

◆ concurrency()

std::size_t ADS::ThreadPool::concurrency ( void ) const
inlinenodiscard

the number of threads, the caller included

◆ invoke()

template<class F, class G>
void ADS::ThreadPool::invoke ( F && f,
G && g )

the calls f() and g() in parallel, returning when both have returned

In pseudocode (the leapfrogging of Wagner and Calder, beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{invoke}( f , g ) \cr 2 & \quad \textbf{if}\ p = 1\ \textbf{then}\ f();\ g();\ \textbf{return} \cr 3 & \quad \text{push the task } g \text{ on } D_s;\ \text{wake the } \text{threads that sleep} \cr 4 & \quad f() \cr 5 & \quad t \gets \mathrm{take}( D_s ) \cr 6 & \quad \textbf{if}\ t = g\ \textbf{then}\ g()\quad \triangleright \ \text{not stolen: here} \cr 7 & \quad \textbf{else} \cr 8 & \qquad \textbf{if}\ t \ne \textbf{nil}\ \textbf{then}\ \mathrm{push}( D_s , t )\quad \triangleright\ \text{a task of an enclosing call} \cr 9 & \qquad \textbf{while}\ g \text{ has not returned}\ \textbf{do} \ \text{steal a task and run it, or pause} \cr 10 & \quad \text{throw again the exception of } f \text{, else that of } g \text{, if any} \end{array}} \]

◆ operator=()

ThreadPool & ADS::ThreadPool::operator= ( const ThreadPool & )
delete

no copy, since the workers hold the address of the pool

◆ run()

template<class F>
void ADS::ThreadPool::run ( std::size_t p,
F && f )

the calls \( f( 0 ) , \ldots , f( p - 1 ) \) of long-lived workers, each begun by a thread of the pool that holds no other one, and all at the same time on \( p \) distinct threads if none returns before all have begun; returns when all have returned

In pseudocode (beyond the book), \( c \) the next call to claim and \( d \) the calls of the workers returned:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{run}( p , f ) \cr 2 & \quad \textbf{if}\ p \le 1\ \textbf{then}\ \textbf{for}\ i \gets 0 \ \textbf{to}\ p - 1\ \textbf{do}\ f( i );\ \textbf{return} \cr 3 & \quad \text{under the mutex of the runs: } c \gets 1;\ d \gets 0; \ \text{open the run};\ \text{wake the threads that sleep} \cr 4 & \quad f( 0 ) \cr 5 & \quad \textbf{while}\ d \ne p - 1\ \textbf{do}\ \text{steal a task} \text{ and run it, or pause} \cr 6 & \quad \text{throw again the first exception of the calls, if any} \cr 7 & \textbf{procedure}\ \mathrm{claim}()\quad \triangleright \ \text{by an idle worker, in its loop only} \cr 8 & \quad \text{under the mutex: } \textbf{if}\ \text{the run is } \text{open}\ \textbf{then}\ i \gets c;\ c \gets c + 1; \ \text{close it if } c = p \cr 9 & \quad f( i );\ \mathrm{fetchAndAdd}( d , 1 ) \end{array}} \]

Precondition
\( p \le \) concurrency(), with \( p - 1 \) workers idle

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