recall

← recall

lock ordering pattern

Pick one global order in which all locks must be acquired, and follow it everywhere. Two threads can't deadlock if neither can ever go A→B while the other goes B→A.

Pick one global order in which all locks must be acquired, and follow it everywhere. Two threads can't deadlock if neither can ever go A→B while the other goes B→A.

symptoms

  • deadlocks under load
  • rare hangs that go away on retry

causes

  • different code paths acquiring the same locks in different orders

fixes

  • enforce a documented order (alphabetical, by id, etc.)
  • static analysis or runtime detection
  • minimize multi-lock critical sections

you might say

  • take locks in order
  • deadlock prevention via ordering

related

topics: concurrency, locks

references: