Struct global_counter::primitive::fast::ApproxCounterUsize [−][src]
A global approximate counter.
This counter operates by having a local counter for each thread, which is occasionally flushed to the main global counter.
The accuracy of the counter is determined by its resolution
and the number of threads counting on it:
The value returned by get
is guaranteed to always be less than or to equal this number of threads multiplied with the resolution minus one
away from the actual amount of times inc
has been called (+ start offset):
|get - (actual + start)| <= num_threads * (resolution - 1)
With resolution being >= 1. This is the only guarantee made.
Setting the resolution to 0 or 1 will just make it a worse primitive counter, don’t do that. Increasing the resolution increases this counters performance.
This counter also features a flush
method, which can be used to manually flush the local counter of the current thread.
Note that this counters inc
- (flush
) - get
- path does not induce a happens-before relationship, just like the flushing counters.
Implementations
impl ApproxCounterUsize
[src]
pub const fn new(start: usize, resolution: usize) -> Self
[src]
Creates a new counter, with the given start value and resolution. Can be used in static contexts.
The start value is a lower bound for the value returned by get
, not guaranteed to be the exact value on subsequent calls.
pub fn inc(&self)
[src]
Increments the counter by one.
Note that this call will probably leave the value returned by get
unchanged.
pub fn get(&self) -> usize
[src]
Gets the current value of the counter. For more information, see the struct-level documentation.
Especially note, that two calls to get
with one inc
interleaved are not guaranteed to, and almost certainely wont, return different values.
pub fn flush(&self)
[src]
Flushes the local counter to the global.
Note that this only means the local counter of the thread calling is flushed. If you want to flush the local counters of multiple threads, each thread needs to call this method.
If every thread which incremented this counter has flushed its local counter, and no other increments have been made nor are being made,
a subsequent call to get
is guaranteed to return the exact count.
However, if you can make use of this, consider if a flushing counter fits your usecase better.
Auto Trait Implementations
impl RefUnwindSafe for ApproxCounterUsize
impl Send for ApproxCounterUsize
impl Sync for ApproxCounterUsize
impl Unpin for ApproxCounterUsize
impl UnwindSafe for ApproxCounterUsize
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
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.
pub 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>,