Crate swmr_cell

Crate swmr_cell 

Source
Expand description

§SWMR Version-Based Single Object

This crate provides a single-writer, multi-reader (SWMR) cell that supports concurrent wait-free reads and lock-free writes using version-based garbage collection.

§Core Concepts

  • Single Object: The swmr_cell library manages a single versioned object per SwmrCell.
  • Version: The version counter represents the state of the object. Each write increments the version.
  • Pinning: Readers pin the current version when they start reading, preventing the writer from reclaiming that version (and any older versions still visible to other readers) until they are done.

§Typical Usage

use swmr_cell::SwmrCell;

// 1. Create a new SWMR cell with an initial value
let mut cell = SwmrCell::new(42i32);

// 2. Create a local reader for this thread (or pass to another thread)
let local = cell.local();

// 3. Pin and read the value by dereferencing the guard
let guard = local.pin();
assert_eq!(*guard, 42);
drop(guard);

// 4. Writer updates the value
cell.store(100i32);

// 5. Read the new value
let guard = local.pin();
assert_eq!(*guard, 100);
drop(guard);

// 6. Manually collect garbage (optional, happens automatically too)
cell.collect();

Structs§

LocalReader
A reader thread’s local version state.
PinGuard
A guard that keeps the current thread pinned to a version.
SwmrCell
A single-writer, multi-reader cell with version-based garbage collection.
SwmrCellBuilder
A builder for configuring and creating a SWMR cell.
SwmrReader
A handle for creating LocalReaders that can be shared across threads.