This question evaluates understanding of concurrency control, deadlock diagnosis, and mutex-based synchronization in multithreaded programs. It is commonly asked to assess reasoning about inter-thread interactions and trade-offs in system design, is categorized under System Design and concurrent programming, and primarily tests practical application of debugging and mitigation strategies rather than purely conceptual theory.
You have concurrent code that acquires multiple locks and occasionally deadlocks. The underlying issue is likely inconsistent lock acquisition order across threads. The goal is to identify the exact deadlock scenario and propose robust fixes. Assume two shared resources protected by two locks (A and B), and that different threads may acquire them in different orders.
std::mutex mA;
std::mutex mB;
void thread1() {
std::unique_lock<std::mutex> lA(mA);
// ... do some work
std::unique_lock<std::mutex> lB(mB);
// critical section using A and B
}
void thread2() {
std::unique_lock<std::mutex> lB(mB);
// ... do some work
std::unique_lock<std::mutex> lA(mA);
// critical section using B and A
}
Login required