Skip to main content

BoundedBackoffStrategy

Trait BoundedBackoffStrategy 

Source
pub trait BoundedBackoffStrategy: BackoffStrategy {
    // Required method
    fn is_completed(&self) -> bool;

    // Provided method
    fn try_backoff_until<C: BackoffUntilCondition, F: FnMut() -> C>(
        &mut self,
        f: F,
    ) -> Option<C::Result> { ... }
}
Expand description

A BackoffStrategy which completes after a bounded number of iterations.

It is typically used to spin a bit before falling back to a slower waiting mechanism, like parking the thread.

§Examples

use std::sync::atomic::{AtomicBool, Ordering::Acquire};

use atomic_backoff::{BackoffLimit, BoundedBackoffStrategy, ExponentialBackoff};

fn wait(flag: &AtomicBool, park: impl Fn()) {
    let mut backoff = BackoffLimit::<ExponentialBackoff<6>, 10>::default();
    // Spin a bit, then park the thread if the flag is still not set.
    while backoff.try_backoff_until(|| flag.load(Acquire)).is_none() {
        park();
    }
}

Required Methods§

Source

fn is_completed(&self) -> bool

Returns true if the bounded number of iterations has been reached.

backoff can still be called afterward.

Provided Methods§

Source

fn try_backoff_until<C: BackoffUntilCondition, F: FnMut() -> C>( &mut self, f: F, ) -> Option<C::Result>

Loops until a condition is satisfied or the backoff is completed, performing backoff at each iteration.

Returns None if the backoff completed before the condition was satisfied.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§