#reference #lazy-evaluation #atomic #synchronization-primitive

lazy_ref

Implements a non-blocking synchronization primitive for lazy-initialized immutable references

3 releases (breaking)

0.4.0 May 9, 2024
0.3.0 May 9, 2024
0.2.0 May 8, 2024
0.1.0 May 8, 2024

#718 in Concurrency

MIT license

12KB
106 lines

lazy_ref

Implements a non-blocking synchronization primitive for lazy-initialized immutable references.

Crates.io Documentation MIT licensed Build Status

Usage

Writing to a LazyRef from separate threads:

use rayon::prelude::*;
use lazy_ref::LazyRef;

let lazy_ref = LazyRef::new();
let thread_ids: Vec<usize> = vec![1, 2, 3];

thread_ids.par_iter()
    .for_each(
        |id| {
           let r = lazy_ref.get_or_init(|| id);
           assert!(thread_ids.contains(r));
       }
    );
let x = lazy_ref.get().unwrap();
assert!(thread_ids.contains(x));

Dependencies

~115KB