Junior — Middle
What is the minimum number of threads required to cause a deadlock?
sobes.tech AI
Answer from AI
The minimum number of threads required for a deadlock to occur is two.
A deadlock occurs when two or more threads hold resources simultaneously and each waits for the resource held by the other. For example:
- Thread A acquires resource 1 and waits for resource 2.
- Thread B acquires resource 2 and waits for resource 1.
Both threads are blocked forever because each is waiting for a resource held by the other.
Pseudo-code example:
// Thread 1
lock(resource1)
wait()
lock(resource2)
// Thread 2
lock(resource2)
wait()
lock(resource1)
Thus, a deadlock requires only two threads and two resources.