7 releases

0.1.0 Nov 16, 2024
0.0.6 Feb 13, 2024
0.0.5 Jan 27, 2024
0.0.4 Nov 28, 2023
0.0.1 Sep 28, 2023

#231 in Concurrency

Download history 7/week @ 2024-09-19 11/week @ 2024-09-26 105/week @ 2024-11-14 10/week @ 2024-11-21

115 downloads per month

MIT license

59KB
964 lines

hzrd

Crate API

Provides shared, mutable state by utilizing hazard pointers.

Usage

The core API of this crate is the HzrdCell, which provides an API reminiscent to that of the standard library's Cell-type. However, HzrdCell allows shared mutation across multiple threads.

use hzrd::HzrdCell;

let cell = HzrdCell::new(false);

std::thread::scope(|s| {
    s.spawn(|| {
        // Loop until the value is true ...
        while !cell.get() {
            std::hint::spin_loop();
        }

        // ... and then set it back to false!
        cell.set(false);
    });

    s.spawn(|| {
        // Set the value to true
        cell.set(true);

        // And then read the value!
        // This might print either `true` or `false`
        println!("{}", cell.get()); 
    });
});

License

This project is licensed under the MIT license.

No runtime deps