Module 7Mutex & Interior MutabilityLesson 5 of 716 min
Lock Hygiene & Deadlocks
Ordering locks and avoiding nested lock footguns.
Deadlock classic
Thread A locks M1 then M2; Thread B locks M2 then M1.
Rules of thumb
- Take locks in a global order
- Prefer one lock
- Prefer message passing for aggregates
- Never hold lock across slow IO
Challenge
Explain why fine-grained per-path locks would serialize MC badly.
Tips
- Global lock order
- One mutex simpler
- Do not serialize hot loops
Try it live
Lock Hygiene & DeadlocksRust 2021
Loading editor…
Output
Press Run to compile and execute.