rust-impl-concurrency
Installation
SKILL.md
rust-impl-concurrency
Thread-safe shared mutable state in Rust is a layered choice. From cheapest to most expensive: atomic (single primitive value, lock-free), RwLock (many readers OR one writer), Mutex (one accessor at a time). Each layer is wrapped in Arc<T> so multiple threads can own the same handle. Picking the wrong layer wastes performance or causes correctness bugs.
Cross-references: [[rust-impl-channels]] (channel pattern is an alternative to shared-state pattern), [[rust-syntax-smart-pointers]] (Arc, Rc, Cell, RefCell semantics), [[rust-core-memory-model]] (Send/Sync auto-trait rules), [[rust-impl-async-tokio]] (tokio::sync::Mutex for async contexts).
When to use this skill
- User wraps shared state in
Arc<Mutex<T>>or asks which sync primitive to pick. - User reads or writes atomic types (
AtomicUsize,AtomicBool, ...) and must choose anOrdering. - User gets a
PoisonErrorand asks how to recover. - User considers
parking_lot::Mutexvsstd::sync::Mutex. - User borrows local data across threads and needs
std::thread::scope(stabilised 1.63). - User asks "why am I deadlocking" (lock-ordering question).
- User asks about
thread_local!storage. - User asks "can I use
Rcbetween threads" (answer: no, useArc).