#lock-free #key #pi #alloter

pi_key_alloter

lock free Key(idx:u32, version:u32) alloter

17 releases (4 breaking)

0.5.1 May 29, 2024
0.4.10 Apr 22, 2024
0.4.5 Mar 8, 2024
0.4.4 Oct 10, 2023

#207 in Concurrency

Download history 64/week @ 2024-06-13 75/week @ 2024-06-20 46/week @ 2024-06-27 31/week @ 2024-07-04 61/week @ 2024-07-11 54/week @ 2024-07-18 42/week @ 2024-07-25 57/week @ 2024-08-01 50/week @ 2024-08-08 45/week @ 2024-08-15 43/week @ 2024-08-22 43/week @ 2024-08-29 41/week @ 2024-09-05 41/week @ 2024-09-12 73/week @ 2024-09-19 44/week @ 2024-09-26

211 downloads per month
Used in 24 crates (3 directly)

MIT/Apache

21KB
500 lines

pi_key_alloter

Crate Github Docs

lock-free Key(idx:u32, version:u32) alloter.

Examples

alloc key:

let alloter = pi_key_alloter::KeyAlloter::new(0);
let k = alloter.alloc();
assert_eq!(0, k.index());
assert_eq!(1, k.version());
alloter.recycle(k);
let k = alloter.alloc();
assert_eq!(0, k.index());
assert_eq!(2, k.version());
let k = alloter.alloc();
assert_eq!(1, k.index());
assert_eq!(1, k.version());

The alloter can be shared across threads with an Arc:

use std::sync::Arc;

fn main() {
    let alloter = Arc::new(pi_key_alloter::KeyAlloter::new());

    // spawn 6 threads that append to the arr
    let threads = (0..6)
        .map(|i| {
            let alloter = alloter.clone();

            std::thread::spawn(move || {
                let _ = alloter.alloc();
            })
        })
        .collect::<Vec<_>>();

    // wait for the threads to finish
    for thread in threads {
        thread.join().unwrap();
    }
    let k = alloter.alloc();
    assert_eq!(6, k.index());
    assert_eq!(1, k.version());
    
}

Dependencies

~280–465KB