-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtry_lock.cpp
More file actions
38 lines (30 loc) · 1.06 KB
/
Copy pathtry_lock.cpp
File metadata and controls
38 lines (30 loc) · 1.06 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
/*
1.try_lock () is different from mutex lock
2.it's a fxn which tries to lock all the lockable object passed to it
e.g std::try_lock(m1,m2,m3,m4..mn);
3. it tries to lock an obkject is successfully locked then return -1 else 0-based indexing which object was not able to locked.
4. if failed to lock any of the given objec then it releases all the prev locked obj.
5.If a call to try_lock results in an exception, unlock is called for any locked objects before rethrowing.
6.std::try_lock() is a special function that tries to lock multiple mutexes at once without blocking.
*
*/
#include<bits/stdc++.h>
using namespace std;
std::mutex mtx1,mtx2;
void solve(){
int res;
while((res=std::try_lock(mtx1,mtx2))!=-1){
cout<<"mutex"<<res<<"failed to lock...trying to lock\n";
std::this_thread::yield();//let other threads run by yield function
}
cout<<"both mutexes locked successfully by thread\n"<<this_thread::get_id()<<endl;
mtx1.unlock();
mtx2.unlock();
}
int main(){
std::thread t1(solve);
std::thread t2(solve);
t1.join();
t2.join();
return 0;
}