ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Backoff.h File Reference

The exponential backoff of a thread that waits for a word of memory to change, beyond the book: the \( k \)-th call of pause() runs \( 2^{\min\{ k , S \}} \) pauses of the processor (pause on x86, yield on AArch64) while \( k \le S \), and from then on gives the processor back to the system (std::this_thread::yield()), so that a wait that ends soon costs no system call and one that lasts lets the other threads run; reset() starts again from one pause, after the wait has ended or the thread has done some work. More...

#include "ADS/Contract.h"
#include "ADS/memory/Prefetch.h"
#include "ADS/concurrent/impl/Spin.tpp"
#include <thread>

Go to the source code of this file.

Classes

class  ADS::Backoff
 the exponential backoff of a waiting thread: \( 2^k \) pauses at the \( k \)-th call up to \( S \), then a yield to the system More...

Namespaces

namespace  ADS

Detailed description

The exponential backoff of a thread that waits for a word of memory to change, beyond the book: the \( k \)-th call of pause() runs \( 2^{\min\{ k , S \}} \) pauses of the processor (pause on x86, yield on AArch64) while \( k \le S \), and from then on gives the processor back to the system (std::this_thread::yield()), so that a wait that ends soon costs no system call and one that lasts lets the other threads run; reset() starts again from one pause, after the wait has ended or the thread has done some work.

With \( S = 6 \), the default, the spinning lasts \( 2^7 - 1 = 127 \) pauses, a few microseconds. The growth by doubling spreads the retries of threads that compete for one line of cache, whose loads and failed compare-and-swaps would otherwise keep the line moving between the caches (Anderson; Mellor-Crummey and Scott); spinning() tells whether the next call still spins, so that a waiter can move on to sleep, e.g., on an EventCount [see EventCount.h], once it yields (see Sleeping until a condition holds).

The contracts [see Contract.h]: \( 0 \le S \le 16 \). There is no other operation to define.

See also
T. E. Anderson, The performance of spin lock alternatives for shared-memory multiprocessors, IEEE Transactions on Parallel and Distributed Systems 1(1), 6-16, 1990, doi:10.1109/71.80120
J. M. Mellor-Crummey, M. L. Scott, Algorithms for scalable synchronization on shared-memory multiprocessors, ACM Transactions on Computer Systems 9(1), 21-65, 1991, doi:10.1145/103727.103729
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa