Struct incr::AtomicIncr
[−]
[src]
pub struct AtomicIncr(_);
AtomicIncr
is a threadsafe, yet very fast counter, utilizing compare
and swap instructions to provide speed and safety in the same package.
There are some cases where 5ns matters. But in many, many other
situations, it's a perfectly good decision to just use the AtomicIncr
,
knowing it can handle anything, and move on to other problems.
Examples
use incr::AtomicIncr; #[cfg(feature = "nightly")] type Atomic = AtomicU64; #[cfg(not(feature = "nightly"))] type Atomic = AtomicUsize; let stop = Arc::new(AtomicBool::new(false)); let last: AtomicIncr = Default::default(); let mut threads = Vec::new(); for _ in 0..5 { let val: Arc<Atomic> = last.clone().into_inner(); let stop = Arc::clone(&stop); threads.push(thread::spawn(move || { loop { val.fetch_add(1, Ordering::Relaxed); thread::yield_now(); if stop.load(Ordering::Relaxed) { break } } })); } let mut i = 1; for _ in 0..100 { i = match last.is_new(i) { true => i + 1, false => i.max(last.get()), }; } stop.store(true, Ordering::SeqCst);
Methods
impl AtomicIncr
[src]
pub fn is_new(&self, val: u64) -> bool
[src]
Returns true
if val
is greater than the highest previously observed
value. If val
is a new maximum, it is stored in self
for checks against
future values subsequent calls Self::get(&self)
will return val
until a
new max is observed.
pub fn get(&self) -> u64
[src]
Returns the current maximum.
pub fn into_inner(self) -> Arc<AtomicU64>
[src]
Consumes the outer struct, returning the inner Arc<Atomic>
.
Trait Implementations
impl Default for AtomicIncr
[src]
fn default() -> AtomicIncr
[src]
Returns the "default value" for a type. Read more
impl Clone for AtomicIncr
[src]
fn clone(&self) -> AtomicIncr
[src]
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more
impl PartialEq for AtomicIncr
[src]
fn eq(&self, other: &Self) -> bool
[src]
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests for !=
.
impl PartialOrd for AtomicIncr
[src]
fn partial_cmp(&self, other: &Self) -> Option<Ordering>
[src]
This method returns an ordering between self
and other
values if one exists. Read more
fn lt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than (for self
and other
) and is used by the <
operator. Read more
fn le(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
fn gt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
fn ge(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more