dark-std
dark-std is an implementation of thread-safe containers with a read-write separation design borrowed from Golang (reads avoid the container write-lock and never contend with each other; writes are serialized and wait for active readers), plus async/blocking utilities.
- defer! (defer macro)
- SyncHashMap (thread-safe HashMap)
- SyncBtreeMap (thread-safe BtreeMap)
- SyncIndexMap (thread-safe IndexMap)
- SyncVec (thread-safe Vec)
- WaitGroup (sync
wait()+ asyncwait_async()) - AtomicDuration (atomic duration)
for example:
Synchronisation model (contention-free reads, serialized writes): reads (
get/iter/dirty_ref/len/contains_key) never take the container's write lock and never contend with each other: each thread registers a reader slot in its own private counter (per-thread QSBR-style) and only touches its own cache line. The slot is registered lazily — the first read from a thread on a container briefly locks the registry to append its counter; afterwards reads are plain atomic increments. A reader arriving while a writer is active spins (yields) until the writer finishes. Writes take a mutex, raise awritingflag and wait until every thread's reader counter is zero before mutating the container in place — O(1)/O(log n), no whole-container copy and noClonerequirement onK/V. The counters useSeqCstordering to close the store-buffering window, so a reader can never read while a writer mutates (verified with Miri against issue #3 reproductions). A read guard makes writers wait until it is dropped, so drop it before calling a write method from the same scope:# use ; # let m = new; # m.insert; let g = m.get.unwrap; assert_eq!; drop; // release the reader slot before writing m.insert;
wait group:
use Duration;
use sleep;
use WaitGroup;
async