Crate sharded_counter

Source
Expand description

A fast and concurrent counter using sharding.

The counter is recommended for the scenario where multiple threads need to allocate unique IDs fast and concurrently. It works by a global CounterPool shared among the threads. Each thread creates a LocalCounter from the pool, which allocates a portion of unique numbers. The numbers can be iterated, and each time they are depleted, the conuter allocates more new numbers from the global pool.

§Usage

The usage is to start by a CounterPool shared among the threads. Each thread calls .counter() to obtain a local counter. The counter itself is an itertor generating unique numbers.

use sharded_counter::CounterPool;
use std::{sync::Arc, thread};

let num_threads = thread::available_parallelism().unwrap().get();
let pool = Arc::new(CounterPool::new());

let handles: Vec<_> = (0..num_threads)
    .map(|_| {
        let pool = pool.clone();

        thread::spawn(move || {
            let _counts: Vec<_> = pool.counter().take(1_000_000).collect();
        })
    })
    .collect();

for handle in handles {
    handle.join().unwrap();
}

Structs§

CounterPool
A pool of counter shards.
LocalCounter
An iterator of unique numbers.

Functions§

default_shard_amount
Computes the default number of shards.