-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutex4.cpp
42 lines (33 loc) · 969 Bytes
/
mutex4.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <thread>
#include <chrono>
#include <mutex>
using namespace std;
struct CriticalData {
mutex mut;
};
void lock(CriticalData &a, CriticalData &b) {
lock_guard<mutex> g1(a.mut);
cout << "get the first mutex" << endl;
this_thread::sleep_for(chrono::milliseconds(1));
lock_guard<mutex> g2(b.mut);
cout << "get the second mutex" << endl;
// do some thing with a and b
}
int main() {
CriticalData c1, c2;
thread a = thread([&] { lock(c1, c2); });
// c1 and c2 in reverse - change the direction of the lock
thread b = thread([&] { lock(c2, c1); });
a.join();
b.join();
// Will result in a deadlock
//
// get the first mutex
// get the first mutex
// --hang
// a waits for b to unlock from the first thread and b waits for a to unlock from the second thread
// Solution can be to correct the direction of lock
// or we can use unique_lock
return 0;
}