Multicore & Synchronization

Which is an example of deadlock?

  1. AThread 1 holds lock A and waits for lock B, while thread 2 holds lock B and waits for lock A
  2. BTwo threads update the same variable without synchronization and corrupt it
  3. COne thread acquires a lock and releases it immediately
Show answer and explanation

Answer: A. Thread 1 holds lock A and waits for lock B, while thread 2 holds lock B and waits for lock A

Deadlock is a state in which threads each hold a resource while waiting for one held by another, so none can proceed. In the example, thread 1 waits for B to be released and thread 2 waits for A, but neither gives up its own lock, so both stop forever. Because they stop without using the CPU, the program simply appears frozen.

  • A. Correct. Each waits on the other forever.
  • B. That is a data race.
  • C. That is normal, harmless use.
Practice this topic Back to topic

More Multicore & Synchronization questions

  1. In a program using several locks, what is the most common way to prevent deadlock?
  2. What does a memory consistency model define?
  3. Which describes sequential consistency?
  4. 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?
  5. What is a characteristic of the x86 memory consistency model (TSO, total store order)?