rust-async
Installation
SKILL.md
Concurrency & Async
Thread Safety
| Error | Cause | Fix |
|---|---|---|
Rc<T> cannot be sent between threads |
Rc is not Send |
Use Arc |
RefCell<T> is not Sync |
Not thread-safe | Use Mutex or RwLock |
Cannot mutate through Arc |
No interior mutability | Arc<Mutex<T>> or Arc<AtomicT> |
Deadlock Prevention
- Lock ordering: Always acquire multiple locks in the same order across all threads
- Self-deadlock: Never lock the same
std::Mutextwice (useparking_lot::ReentrantMutexif needed) - Scope locks tightly: Drop guards as soon as possible
- Atomics for primitives: Use
AtomicBool/AtomicUsizeinstead ofMutex<bool> - Choose memory order carefully:
Relaxed/Acquire/Release/SeqCst