-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmutex.cpp
More file actions
35 lines (29 loc) · 676 Bytes
/
Copy pathmutex.cpp
File metadata and controls
35 lines (29 loc) · 676 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
#include <iostream>
#include <thread>
#include <mutex>
using namespace std;
std::mutex g_lock;
void func()
{
g_lock.lock();
cout << "in id:" << this_thread::get_id() << endl;
this_thread::sleep_for(std::chrono::seconds(1));
cout << "out id:" << this_thread::get_id() << endl;
g_lock.unlock();
}
void f()
{
lock_guard<std::mutex> lock(g_lock);
cout << "in id:" << this_thread::get_id() << endl;
this_thread::sleep_for(std::chrono::seconds(1));
cout << "out id:" << this_thread::get_id() << endl;
}
int main()
{
std::thread t1(func);
std::thread t2(func);
std::thread t3(func);
t1.join();
t2.join();
t3.join();
}