[][src]Struct conquer_util::BackOff

pub struct BackOff { /* fields omitted */ }

A type for exponential back-off in tight loops.

In concurrent environments it can often be beneficial to back off from accessing shared variables in loops in order to reduce contention and improve performance for all participating threads by spinning for a short amount of time.

Implementations

impl BackOff[src]

pub const fn new() -> Self[src]

Creates a new BackOff instance with a fixed exponential back-off strategy.

pub fn spin_once()[src]

Spin once.

This is a convenience wrapper for spin_loop_hint, but will never compile to only a nop on platforms, that don't offer a wait-like CPU instruction, but will instead result in an empty function call.

pub fn reset(&self)[src]

Resets the BackOff instance to its initial state.

pub fn spin(&self)[src]

Spins for a bounded number of steps

On CPUs that support such instructions, in each step the processor will be instructed to deliberately slow down, e.g. using the pause instruction on x86, which can also save energy.

Each invocation of this method exponentially increases the number of spin cycles until a point at which further spinning is no longer advisable and other strategies, such as yielding the current thread to the OS, should be preferred. From this point on, the number of spin cycles remains constant with each further invocation of spin.

Whether this point has been reached can be determined through the advise_yield method.

pub fn advise_yield(&self) -> bool[src]

Returns true if further spinning is not advisable and other means such as voluntarily yielding the current thread could be more efficient.

Examples

Back-off exponentially until it is no longer advisable.

use conquer_util::BackOff;

let mut backoff = BackOff::new();
while !backoff.advise_yield() {
    backoff.spin();
}

Repedeatly check a condition and either back-off exponentially or yield the current thread, if the condition is not yet met.

use conquer_util::BackOff;


let mut backoff = BackOff::new();
while !cond {
    if backoff.advise_yield() {
        std::thread::yield_now();
    } else {
        backoff.spin();
    }
}

Notes

On an Intel(R) i5 with 2.60 GHz a full back-off cycle has been measured to take approximately 750 nanoseconds

impl BackOff[src]

pub fn random() -> Self[src]

Creates a new BackOff instance with a randomized exponential back-off strategy.

pub fn random_with_seed(seed: u64) -> Self[src]

Creates a new BackOff instance with a randomized exponential back-off strategy using the given seed value.

impl BackOff[src]

pub fn spin_for(dur: Duration)[src]

Spins at least for the specified dur.

If a very short duration is specified, this function may spin for a longer, platform-specific minimum time.

pub fn yield_now()[src]

Cooperatively yields the current thread.

This is a convenience wrapper for thread::yield_now

Trait Implementations

impl Clone for BackOff[src]

impl Debug for BackOff[src]

impl Default for BackOff[src]

impl Display for BackOff[src]

Auto Trait Implementations

impl !RefUnwindSafe for BackOff

impl Send for BackOff

impl !Sync for BackOff

impl Unpin for BackOff

impl UnwindSafe for BackOff

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,