Multicore & Synchronization

In a program using several locks, what is the most common way to prevent deadlock?

  1. AFix a single global order in which every thread acquires locks
  2. BNever release a lock once acquired
  3. CLimit the program to two threads or fewer
Show answer and explanation

Answer: A. Fix a single global order in which every thread acquires locks

Deadlock arises when threads form a cycle, each waiting for a lock held by another. If everyone follows "always take A before B", no thread can hold B while waiting for A, so no cycle forms. Other methods include acquiring all needed locks at once, or releasing held locks and retrying when a lock cannot be obtained.

  • A. Correct. No cycle of waiting can form.
  • B. Without release, other threads wait forever.
  • C. Deadlock can happen with just two threads.