Crate hzrd

source ·
Expand description

This crate provides a safe API for shared mutability using hazard pointers for memory reclamation.

§HzrdCell

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.

The main advantage of HzrdCell, compared to something like a Mutex, is that reading and writing to the value is lock-free. This is offset by an increased memory use, an significant overhead and additional indirection. Here is an example of HzrdCell in use.

use std::time::Duration;
use std::thread;

use hzrd::HzrdCell;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum State {
    Idle,
    Running,
    Finished,
}

let state = HzrdCell::new(State::Idle);

thread::scope(|s| {
    s.spawn(|| {
        thread::sleep(Duration::from_millis(1));
        match state.get() {
            State::Idle => println!("Waiting is boring, ugh"),
            State::Running => println!("Let's go!"),
            State::Finished => println!("We got here too late :/"),
        }
    });

    s.spawn(|| {
        state.set(State::Running);
        thread::sleep(Duration::from_millis(1));
        state.set(State::Finished);
    });
});

assert_eq!(state.get(), State::Finished);

Re-exports§

Modules§

  • Module containing core functionality for this crate.
  • Module containing various types implementing the Domain-trait.

Structs§