-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync.cpp
40 lines (34 loc) · 851 Bytes
/
async.cpp
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
#include <mutex>
#include <thread>
#include <future>
#include <iostream>
std::mutex lock;
void t1()
{
std::lock_guard<std::mutex> l(lock);
std::cout << "t1 " << std::this_thread::get_id() << "\n";
}
void t2()
{
std::lock_guard<std::mutex> l(lock);
std::cout << "t2 " << std::this_thread::get_id() << "\n";
}
void t3()
{
std::lock_guard<std::mutex> l(lock);
std::cout << "t3 " << std::this_thread::get_id() << "\n";
}
int main()
{
auto f1 = std::async(std::launch::async, t1);
auto f2 = std::async(std::launch::deferred, t2);
auto f3 = std::async(t3);
{
std::lock_guard<std::mutex> l(lock);
std::cout << "main " << std::this_thread::get_id() << "\n";
}
f1.get();
f2.get();
f3.get();
return 0;
}