#lock-free #static #lazy-evaluation #once

no-std lock-free-static

Lock-free static variables

5 releases

new 0.2.1 Nov 28, 2024
0.2.0 Oct 26, 2023
0.1.0 Oct 2, 2023

#743 in Concurrency

Download history 2/week @ 2024-08-01 10/week @ 2024-09-19 6/week @ 2024-09-26

275 downloads per month

MIT/Apache

21KB
437 lines

lock-free-static

Crates.io Docs.rs Gitlab CI License

Lock-free static variables.

Examples

Static variable

use lock_free_static::lock_free_static;

lock_free_static!{
    static VAR: i32 = 123;
}

fn main() {
    assert!(VAR.init());
    assert_eq!(*VAR.get().unwrap(), 123);
}

Mutable static variable

use lock_free_static::lock_free_static;

lock_free_static!{
    static mut VAR: i32 = 123;
}

fn main() {
    assert!(VAR.init());

    let mut guard = VAR.lock().unwrap();
    assert_eq!(*guard, 123);
    *guard = 321;
    drop(guard);

    assert_eq!(*VAR.lock().unwrap(), 321);
}

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

No runtime deps