Expand description
bimodal_array provides dual-mode access to a contiguous array. Access
is granted through two handler types:
ElementHandle<T>grants mutable access to a single element.ArrayHandle<T>grants exclusive mutable access to the entire array.
Handles represent the ability to attempt a lock. The actual lock is held by the returned guards:
- when
ElementHandle::lock()succeeds, it produces anElementGuard<T> - when
ArrayHandle::lock()succeeds, it produces anArrayGuard<T>.
Each ElementHandle is bound to a specific element. Guards for different
elements may may be held concurrently. An ArrayGuard<T> provides
exclusive access to the entire array and cannot coexist with any
ElementGuard<T>.
This pattern is useful for scatter–gather style workloads where independent
workers update elements in parallel, followed by an orchestration step that
processes the entire array. Internally, lock state is coordinated via a
single AtomicUsize. As a result, bimodal_array may not scale well under
high contention.
§Examples
const WORKER_COUNT: usize = 8;
let data = vec![0usize; WORKER_COUNT];
let (mut array_handle, element_handles) = bimodal_array(data);
let threads: Vec<_> = element_handles
.into_iter()
.enumerate()
.map(|(idx, mut element)| {
thread::spawn(move || {
let mut guard = element.lock().unwrap();
*guard = 10 - idx;
})
})
.collect();
threads.into_iter().for_each(|t| t.join().unwrap());
let mut array_guard = array_handle.lock().unwrap();
array_guard.sort();Structs§
- Array
Guard - A guard providing exclusive access to the entire array.
- Array
Handle - A handle granting array-wide access to a
bimodal_array. - Element
Guard - A guard providing mutable access to a single element.
- Element
Handle - A handle granting access to a single element of a
bimodal_array.
Enums§
- Bimodal
Array Error - Errors that can occur when constructing or using a
bimodal_array.
Functions§
- bimodal_
array - Constructs a bimodal array from an owned
Vec<T>. - try_
bimodal_ array - Constructs a bimodal array from an owned
Vec<T>.