Expand description
§atomic-backoff
Customizable backoff strategies for compare-and-swap loops and spin loops.
Compare-and-swap (CAS) loops and spin loops can often be optimized by adding backoff at each iteration, i.e. waiting a bit before the next iteration, in order to reduce the contention on the CPU’s cache lines.
As the optimal backoff strategy depends on multiple factors, especially the expected
contention, this crate provides a generic BackoffStrategy to help customize algorithms
using CAS/spin loops. Typical backoff strategies like ExponentialBackoff are also provided.
Atomic types are extended with try_update_with_backoff/update_with_backoff methods,
mirroring their std try_update/update counterparts.
For handwritten CAS loops, see BackoffStrategy::backoff_reload and BackoffState;
for spin loops, see BackoffStrategy::backoff_until.
§Examples
use std::{
sync::atomic::{AtomicUsize, Ordering::Relaxed},
thread,
time::{Duration, Instant},
};
use atomic_backoff::{AtomicWithBackoffExt, BackoffStrategy, ExponentialBackoff, NoBackoff};
fn parallel_increment<S: BackoffStrategy>(threads: usize, iterations: usize) -> Duration {
let counter = AtomicUsize::new(0);
let start = Instant::now();
thread::scope(|s| {
for _ in 0..threads {
s.spawn(|| {
for _ in 0..iterations {
counter.update_with_backoff(Relaxed, Relaxed, |x| x + 1, S::default());
}
});
}
});
assert_eq!(counter.load(Relaxed), threads * iterations);
start.elapsed()
}
let no_backoff = parallel_increment::<NoBackoff>(4, 10_000);
let exponential = parallel_increment::<ExponentialBackoff<6, 4>>(4, 10_000);
println!("no backoff: {no_backoff:?}, exponential backoff: {exponential:?}");
// no backoff: 2.08ms, exponential backoff: 646µsStructs§
- Backoff
State - A wrapper around a
BackoffStrategyto be used in CAS loops when the atomic value must be checked after each reload. - Exponential
Backoff - Performs exponential backoff.
- NoBackoff
- No backoff.
- Spin
Backoff - Emits a
spin_loopand reloads the atomic value before retrying the CAS.
Enums§
- Retry
Strategy - Retry strategy of a failed atomic compare-and-swap (CAS).
Traits§
- Atomic
- An atomic type.
- Atomic
With Backoff Ext - Extension trait providing CAS loop methods using a given
BackoffStrategy. - Backoff
Strategy - Backoff strategy to be used after an atomic compare-and-swap (CAS) failure and in spin loops.
- Backoff
Until Condition - A condition checked in
BackoffStrategy::backoff_until.