Understanding C++ lock() and try_lock() Functions
1 Deadlock std::mutex mtM,mtN;int M = 0;int N = 0; void threadA() { std::lock_guard guardM(mtM); std::lock_guard guardN(mtN); M++; N–;} void threadB() { std::lock_guard guardN(mtN); std::lock_guard guardM(mtM); M–; N++;} This is a typical example that can lead to a deadlock. The classic two-phase locking issue is immediately apparent, but when more locks are involved, the problem … Read more