Expand description
This crate provides a safe API for shared mutability, using hazard pointers for memory reclamation.
§Hazard pointers
Hazard pointers is a strategy for controlled memory reclamation in multithreaded contexts. All readers/writers have shared access to some data, as well as a collection of garbage, and a list of hazard pointers. Whenever you read the value of the data you get a reference to it, which you also store in one of the hazard pointers. Writing to the data is done by swapping out the old value for a new one; the old value is then “retired” (thrown in the pile of garbage). Retired values are only reclaimed if no hazard pointers contain their address. In this way the hazard pointers end up protecting any references from becoming invalid.
§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.
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());
});
});
Modules§
- core
- Module containing core functionality for this crate.
- domains
- Module containing various types implementing the
Domain
-trait.
Structs§
- Hzrd
Cell - Holds a value protected by hazard pointers.
- Hzrd
Reader - A reader object for a specific
HzrdCell