-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeadLock.cpp
More file actions
44 lines (35 loc) · 709 Bytes
/
DeadLock.cpp
File metadata and controls
44 lines (35 loc) · 709 Bytes
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
// DeadLock With Example In C++
#include<iostream>
#include<mutex>
#include<thread>
#include<chrono>
std::mutex m1;
std::mutex m2;
using namespace std;
void Thread1()
{
m1.lock();
std::this_thread::sleep_for(std::chrono::seconds(1));
m2.lock();
cout<<"Critical section of Thread 1"<<endl;
m1.unlock();
m2.unlock();
}
void Thread2()
{
m2.lock();
std::this_thread::sleep_for(std::chrono::seconds(1));
m1.lock();
cout<<"Critical section of Thread 2"<<endl;
m2.unlock();
m1.unlock();
}
int main()
{
std::thread t1(Thread1);
std::thread t2(Thread2);
t1.join();
t2.join();
return 0;
}
// compile : g++ DeadLock.cpp -std=c++11 -pthread