Struct atomic_interval::AtomicInterval [−][src]
pub struct AtomicInterval { /* fields omitted */ }Expand description
It implements a timer. It allows checking a periodic interval.
This structure is meant to be shared across multiple threads and does not
require additional sync wrappers. Generally, it can be used with
Arc<AtomicInterval>.
If you want performance maximization (when you do not need memory ordering
synchronization), AtomicIntervalLight is a relaxed variant of this class.
Implementations
Creates a new AtomicInterval with a fixed period interval.
The first tick is not instantaneous at the creation of the interval. It means period
amount of time has to elapsed for the first tick.
Checks whether the interval’s tick expired.
When it returns true then at least period amount of time has passed
since the last tick.
When a period is passed (i.e., this function return true) the internal timer
is automatically reset for the next tick.
It takes two Ordering arguments to describe the memory ordering.
success describes the required ordering when the period elapsed and the timer
has to be reset (read-modify-write operation).
failures describes the required ordering when the period is not passed yet.
Using Ordering::Acquire as success ordering makes the store part of this operation
Ordering::Relaxed, and using Ordering::Release makes the successful load
Ordering::Relaxed.
The failure ordering can only be Ordering::SeqCst, Ordering::Acquire or
Ordering::Relaxed and must be equivalent to or weaker than the success ordering.
It can be used in a concurrency context: only one thread can tick the timer per period.
Example
use atomic_interval::AtomicInterval;
use std::sync::atomic::Ordering;
use std::time::Duration;
use std::time::Instant;
let atomic_interval = AtomicInterval::new(Duration::from_secs(1));
let time_start = Instant::now();
let elapsed = loop {
if atomic_interval.is_ticked(Ordering::Relaxed, Ordering::Relaxed) {
break time_start.elapsed();
}
};
println!("Elapsed: {:?}", elapsed);
// Elapsed: 999.842446ms