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.
Practice this topic Back to topic

More Multicore & Synchronization questions

  1. What does a memory consistency model define?
  2. Which describes sequential consistency?
  3. The two threads above run simultaneously on different cores (assume no compiler reordering). Which outcome cannot occur under sequential consistency but can occur on real CPUs such as x86?
  4. What is a characteristic of the x86 memory consistency model (TSO, total store order)?
  5. Which statement is correct about weak memory consistency models such as those of Arm and RISC-V (RVWMO)?