-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10_uniqueLock.cpp
More file actions
119 lines (107 loc) · 2.32 KB
/
Copy path10_uniqueLock.cpp
File metadata and controls
119 lines (107 loc) · 2.32 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// TOPIC: unique_lock In C++ (std::unique_lock<mutex> lock(m1))
// NOTES:
// 1. The class unique_lock is a mutex ownership wrapper.
// 2. It Allows:
// a. Can Have Different Locking Strategies
// b. time-constrained attempts at locking (try_lock_for, try_lock_until)
// c. recursive locking
// d. transfer of lock ownership (move not copy)
// e. condition variables. (See this in coming videos)
// Locking Strategies
// TYPE EFFECTS(S)
// 1. defer_lock do not acquire ownership of the mutex.
// 2. try_to_lock try to acquire ownership of the mutex without blocking.
// 3. adopt_lock assume the calling thread already has ownership of the mutex.
#include<iostream>
#include<thread>
#include<mutex>
using namespace std;
mutex m1;
int buffer = 0;
void task(const char* c,int loopFor){
unique_lock<mutex> lock(m1); //Automatically calls lock on mutex m1.
// m1.lock();
for(int i=0;i<loopFor;i++){
buffer++;
cout<<c<<buffer<<endl;
}
// m1.unlock();
}
int main(){
thread t1(task,"T1 ",10);
thread t2(task,"T2 ",10);
t1.join();
t2.join();
return 0;
}
/*
<-----Output------>
deepankerrawat@MacBookAir Multi_Threading % ./a.out
T1 1
T1 2
T1 3
T1 4
T1 5
T1 6
T1 7
T1 8
T1 9
T1 10
T2 11
T2 12
T2 13
T2 14
T2 15
T2 16
T2 17
T2 18
T2 19
T2 20
*/
// EXAMPLE: 2
/*
For defer lock:-
unique_lock<mutex> lock(m1,defer_lock); //Does not call lock on mutex m1, because used defer_lock .
// we can also write no of line codes before lock.
lock.lock(); //But then we will have to explicitly tell to lock when ever we want lock mutex m1.
*/
void task(const char* threadNumber, int loopFor) {
std::unique_lock<mutex> lock(m1, std::defer_lock); // Does not call lock on mutex m1, because used defer_lock
lock.lock(); // But then we will have to explicitly tell to lock when ever we want to lock mutex m1.
for(int i=0; i<loopFor; ++i){
buffer++;
cout << threadNumber << buffer << endl;
}
// lock.unlock(); is not needed as it will be unlocked in destructor of unique_lock.
}
int main() {
thread t1(task, "T1 ", 10);
thread t2(task, "T2 ", 10);
t1.join();
t2.join();
return 0;
}
/*
<-------Output------->
deepankerrawat@MacBookAir Multi_Threading % ./a.out
T1 1
T1 2
T1 3
T1 4
T1 5
T1 6
T1 7
T1 8
T1 9
T1 10
T2 11
T2 12
T2 13
T2 14
T2 15
T2 16
T2 17
T2 18
T2 19
T2 20
*/