pub struct SyncBtreeMap<K: Eq + Hash, V> { /* private fields */ }Expand description
this sync map used to many reader,writer less.space-for-time strategy
Reads are lock-free: get/iter/dirty_ref/len/contains_key only
register a reader slot with an atomic counter and then read the map without
any lock (readers never block each other and never touch a lock word).
Writes take a mutex, raise a writing flag and wait until all in-flight
readers are gone before mutating the map in place — O(log n), no
whole-container copy and no Clone requirement on K/V.
§Deadlock note
A read guard makes writers wait until it is dropped. Do not call a write
method while a read/write guard is alive in the same scope: drop the guard
first (e.g. drop(g) before insert/remove/get_mut), otherwise the
writer waits for its own guard and deadlocks.
Implementations§
Source§impl<K, V> SyncBtreeMap<K, V>
impl<K, V> SyncBtreeMap<K, V>
pub fn new_arc() -> Arc<Self> ⓘ
pub fn new() -> Self
pub fn with_capacity(_capacity: usize) -> Self
pub fn with_map(map: BTreeMap<K, V>) -> Selfwhere
K: Ord,
pub fn insert(&self, k: K, v: V) -> Option<V>where
K: Ord,
pub fn insert_mut(&mut self, k: K, v: V) -> Option<V>where
K: Ord,
pub fn remove(&self, k: &K) -> Option<V>where
K: Ord,
pub fn remove_mut(&mut self, k: &K) -> Option<V>where
K: Ord,
pub fn len(&self) -> usize
pub fn is_empty(&self) -> bool
pub fn clear(&self)
pub fn clear_mut(&mut self)
pub fn shrink_to_fit(&self)
pub fn shrink_to_fit_mut(&mut self)
pub fn from(map: BTreeMap<K, V>) -> Self
Sourcepub fn get<Q>(&self, k: &Q) -> Option<BtreeMapGet<'_, V>>
pub fn get<Q>(&self, k: &Q) -> Option<BtreeMapGet<'_, V>>
Returns a read-guarded reference to the value corresponding to the key.
The key may be any borrowed form of the map’s key type.
The read is lock-free: it only registers a reader slot, so concurrent reads never block each other and never take a lock. Writers wait for the returned guard to be dropped before mutating the map.
§Examples
use dark_std::sync::{SyncBtreeMap};
let mut map = SyncBtreeMap::new();
map.insert_mut(1, "a");
assert_eq!(*map.get(&1).unwrap(), "a");
assert_eq!(map.get(&2).is_none(), true);Sourcepub fn get_mut(&self, k: &K) -> Option<BtreeMapRefMut<'_, K, V>>where
K: Ord,
pub fn get_mut(&self, k: &K) -> Option<BtreeMapRefMut<'_, K, V>>where
K: Ord,
Returns a write-guarded mutable reference to the value of the key.
The guard holds the writer lock (writers are mutually exclusive and wait for in-flight readers) until it is dropped, so the mutable reference can never race with concurrent readers or writers. Drop it before calling another method from the same scope.