Rs
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

  1. Take locks in a global order
  1. Prefer one lock
  1. Prefer message passing for aggregates
  1. 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.