pi_key_alloter

Crates.iopi_key_alloter
lib.rspi_key_alloter
version0.5.1
sourcesrc
created_at2023-08-29 11:55:37.046571
updated_at2024-05-29 09:27:57.626193
descriptionlock free Key(idx:u32, version:u32) alloter
homepage
repositoryhttps://github.com/GaiaWorld/pi_key_alloter.git
max_upload_size
id957965
size20,140
dev (github:gaiaworld:dev)

documentation

README

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());
    
}
Commit count: 23

cargo fmt