Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Guides design and review of Rust thread concurrency, choosing from scoped threads, channels, Arc, Mutex, RwLock, Condvar, atomics, OnceLock.
86% 95%+9 ptsof test cases passed
Passed 21 of 22 test cases with the skill, and 19 of 22 without it.
I am writing a custom thread-safe sequence generator using `AtomicU64` in Rust. I want to optimize performance by using `Ordering::Relaxed` everywhere across my `fetch_add` operations to minimize CPU barrier instructions. What memory ordering strategy should I start with?
Advises using SeqCst by default before relaxing atomic memory ordering.
3 of 3 safety checks passed
Use this skill to design thread-based Rust concurrency with explicit ownership, sharing, and synchronization. Prefer the simplest primitive that matches the coordination requirement before adding shared mutable state.
std::thread::scope when child threads can borrow stack data and must
finish before the function returns.Arc<Mutex<T>> or
Arc<RwLock<T>> only when shared state is the clearer model.OnceLock or LazyLock for thread-safe one-time initialization instead
of ad hoc global mutable state.SeqCst by default until a weaker
ordering is justified and documented.Read references/threading-shared-state.md before introducing a new shared
state primitive or reviewing deadlock-prone code.
| Need | Primitive |
|---|---|
| Borrow local data into short-lived threads | std::thread::scope |
| Transfer work or results | std::sync::mpsc or project channel crate |
| Shared read/write state | Arc<Mutex<T>> |
| Many readers, rare writers | Arc<RwLock<T>> |
| Wait for condition changes | Condvar with Mutex |
| One-time global initialization | LazyLock or OnceLock |
| Counters, flags, lock-free coordination | std::sync::atomic |
Next to this one in the catalogue, plus more from the same author.