Repository navigation
Expand file tree
/
Copy pathDeadLockSolution4.cpp
More file actions
66 lines (58 loc) · 1.84 KB
/
Copy pathDeadLockSolution4.cpp
File metadata and controls
66 lines (58 loc) · 1.84 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <cstdlib>
#include <thread>
#include <mutex>
using namespace std;
//For simplicity, entire code is put in single file
//It will avoid the deadlock seen in DeadLockProducer.
//DeadLockSolution4 - Deadlock avoidance by using C++17 - std::scoped_lock() - RAII and lock in one go.
class DeadLockSolution4
{
public:
void thread_func();
void displayFromThread1(const string& sThreadName, const int& iCounter);
void displayFromThread2(const string& sThreadName, const int& iCounter);
private:
std::mutex mutex1;
std::mutex mutex2;
};
void DeadLockSolution4::displayFromThread1(const string& sThreadName, const int& iCounter)
{
//1. Lock mutex1 first then mutex2 - by using C++17 std::scoped_lock()
//It also provides a convenient RAII-style mechanism
//It will avoid deadlock seen in DeadLockProducer
std::scoped_lock<std::mutex, std::mutex>slock(mutex1, mutex2);
cout<< sThreadName << " : " << iCounter <<endl;
}
void DeadLockSolution4::displayFromThread2(const string& sThreadName, const int& iCounter)
{
//2. Lock mutex1 first then mutex2 - by using C++17 std::scoped_lock()
//It also provides a convenient RAII-style mechanism
//It will avoid deadlock seen in DeadLockProducer
std::scoped_lock<std::mutex, std::mutex>slock(mutex1, mutex2);
cout<< sThreadName << " : " << iCounter <<endl;
}
void DeadLockSolution4::thread_func()
{
cout<< "In thread 2: " << endl;
int i=0;
while( i<=100 )
{
displayFromThread2("Thread-2", i);
++i;
}
}
int main(int argc, char** argv)
{
DeadLockSolution4 ds1;
thread th1(&DeadLockSolution4::thread_func, &ds1 );
cout<< "In main thread: " << endl;
int i=200;
while( i<=300 )
{
ds1.displayFromThread1("Thread-1", i);
++i;
}
th1.join();
return EXIT_SUCCESS;
}