pub struct ConcurrentCounter { /* private fields */ }
Expand description
A sharded atomic counter
ConcurrentCounter shards cacheline aligned AtomicIsizes across a vector for faster updates in a high contention scenarios.
Implementations§
Source§impl ConcurrentCounter
impl ConcurrentCounter
Sourcepub fn new(count: usize) -> Self
pub fn new(count: usize) -> Self
Creates a new ConcurrentCounter with a minimum of the count
cells. Concurrent counter
will align the count
to the next power of two for better speed when doing the modulus.
§Examples
use fast_counter::ConcurrentCounter;
let counter = ConcurrentCounter::new(10);
Sourcepub fn add(&self, value: isize)
pub fn add(&self, value: isize)
Adds the value to the counter, internally with is using add_with_ordering
with a
Ordering::Relaxed
and is mainly for convenience.
ConcurrentCounter will identify a cell to add the value
too with using a thread_local
which will try to aleviate the contention on a single number
§Examples
use fast_counter::ConcurrentCounter;
let counter = ConcurrentCounter::new(10);
counter.add(1);
counter.add(-1);
Sourcepub fn add_with_ordering(&self, value: isize, ordering: Ordering)
pub fn add_with_ordering(&self, value: isize, ordering: Ordering)
ConcurrentCounter will identify a cell to add the value
too with using a thread_local
which will try to aleviate the contention on a single number. The cell will be updated
atomically using the ordering provided in ordering
§Examples
use fast_counter::ConcurrentCounter;
use std::sync::atomic::Ordering;
let counter = ConcurrentCounter::new(10);
counter.add_with_ordering(1, Ordering::SeqCst);
counter.add_with_ordering(-1, Ordering::Relaxed);
Sourcepub fn sum(&self) -> isize
pub fn sum(&self) -> isize
This will fetch the sum of the concurrent counter be iterating through each of the cells
and loading the values. Internally this uses sum_with_ordering
with a Relaxed
ordering.
Due to the fact the cells are sharded and the concurrent nature of the library this sum may be slightly inaccurate. For example if used in a concurrent map and using ConcurrentCounter to track the length, depending on the ordering the length may be returned as a negative value.
§Examples
use fast_counter::ConcurrentCounter;
let counter = ConcurrentCounter::new(10);
counter.add(1);
let sum = counter.sum();
assert_eq!(sum, 1);
Sourcepub fn sum_with_ordering(&self, ordering: Ordering) -> isize
pub fn sum_with_ordering(&self, ordering: Ordering) -> isize
This will fetch the sum of the concurrent counter be iterating through each of the cells
and loading the values with the ordering defined by ordering
.
Due to the fact the cells are sharded and the concurrent nature of the library this sum may be slightly inaccurate. For example if used in a concurrent map and using ConcurrentCounter to track the length, depending on the ordering the length may be returned as a negative value.
§Examples
use std::sync::atomic::Ordering;
use fast_counter::ConcurrentCounter;
let counter = ConcurrentCounter::new(10);
counter.add(1);
let sum = counter.sum_with_ordering(Ordering::SeqCst);
assert_eq!(sum, 1);