[−][src]Struct conquer_util::BackOff
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.
Methods
impl BackOff
[src]
pub const fn new() -> Self
[src]
Creates a new BackOff
instance with a fixed exponential back-off
strategy.
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
.
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() { BackOff::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
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 merely a convenience wrapper for
thread::yield_now
Trait Implementations
impl Default for BackOff
[src]
impl Clone for BackOff
[src]
fn clone(&self) -> BackOff
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more
impl Debug for BackOff
[src]
impl Display for BackOff
[src]
Auto Trait Implementations
impl Unpin for BackOff
impl Send for BackOff
impl !Sync for BackOff
impl !RefUnwindSafe for BackOff
impl UnwindSafe for BackOff
Blanket Implementations
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToString for T where
T: Display + ?Sized,
[src]
T: Display + ?Sized,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
V: MultiLane<T>,