Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: minor editorial changes

Mutexes are used to prevent multiple threads from causing a data race by accessing the same shared resource at the same time. Sometimes, when locking mutexes, multiple threads hold each other's lock, and the program consequently deadlocks. Four conditions are required for deadlock to occur:

  • Mutual mutual exclusion (at At least one nonshareable resource must be held.),
  • Hold hold and wait (a A thread must hold a resource while awaiting availability of another resource.),
  • No no preemption (resources Resources cannot be taken away from a thread while they are in-use.), and
  • Circular circular wait (a A thread must await a resource held by another thread which is, in turn, awaiting a resource held by the first thread.).

Deadlock needs all four conditions, so preventing deadlock requires preventing any one of the four conditions. One simple solution is to lock the mutexes in a predefined order, which prevents circular wait.

...