-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMutex_try_lock.cpp
More file actions
42 lines (29 loc) · 1.25 KB
/
Copy pathMutex_try_lock.cpp
File metadata and controls
42 lines (29 loc) · 1.25 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
// TOPIC: std :: try_lock() on Mutex in C++11 Threading
// 0. try_lock(),Tries to lock the mutex .Returns immediately . On Successful lock acquisition returns true otherwise returns false.
// 1. If try lock() is not able to lock mutex, then it doesn't get blocked that's why it is called non-blocking.
// 2.If try_lock is called again by the same thread which owns the mutex, the behavious is undefined.
// It is a lock situation wiht undefined behaviour. (if you want to be able to lock the same mutex by same
// thread more than one time the go for recursive_mutex)
#include<iostream>
#include<thread>
#include<mutex>
using namespace std;
int counter=0;
std:: mutex mtx;
void increaseTheCounterFor100000Times(){
for(int i =0;i<100000;i++){
if(mtx.try_lock()){
++counter;
mtx.unlock(); //releasing the lock
}
}
}
int main(){
std::thread t1(increaseTheCounterFor100000Times);
std:: thread t2(increaseTheCounterFor100000Times);
t1.join();
t2.join();
cout<<"counter could increase upto "<<counter<<endl; // the value of counter will always be less than 200000,
// as t1 and t2 cannot increment counter simulaneously , if one is incrementing then other cannot be get locked to increment counter value.
return 0;
}