Review the following code. What potential concurrency issue does it demonstrate?

Go & Rust interview question for Advanced practice.

Answer

A deadlock, because the threads acquire the locks in opposite orders.

Explanation

The correct answer is C. This code is a classic example of a deadlock. Thread h1 locks m1 and then tries to lock m2. Simultaneously, thread h2 locks m2 and then tries to lock m1. If the timing is right, h1 will hold the lock for m1 and wait for m2, while h2 holds the lock for m2 and waits for m1. Neither can proceed, and they will wait forever. Why B is incorrect: The Mutex prevents data races. The problem is a higher-level logical error, not a race condition. Why D is incorrect: No thread is panicking here, so mutex poisoning is not the issue.

Related Questions