-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathc30_lock_guard.cpp
More file actions
37 lines (33 loc) · 872 Bytes
/
Copy pathc30_lock_guard.cpp
File metadata and controls
37 lines (33 loc) · 872 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
#include <chrono>
#include <iostream>
#include <mutex>
#include <string>
#include <thread>
std::mutex m;
void foo(std::string str)
{
try
{
// std::lock_guard<std::mutex> l(m);
// use unique lock instead so that the mutex can be unlocked
// immediately after the critical section
std::unique_lock<std::mutex> l(m);
// more: https://en.cppreference.com/w/cpp/thread/lock_tag
std::cout << str[0] << " " << str[1] << " " << str[2] << std::endl;
l.unlock();
// throw std::exception();
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
catch(const std::exception& e)
{
std::cerr << "Exception occurred: " << e.what() << '\n';
}
}
int main()
{
std::thread t1{foo, "noweg"};
std::thread t2{foo, "dgoyw"};
t1.join();
t2.join();
return 0;
}