-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlisting_4.6.cpp
48 lines (41 loc) · 1.08 KB
/
listing_4.6.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
41
42
43
44
45
46
47
48
#include <future>
#include <iostream>
int find_the_answer_to_ltuae()
{
return 42;
}
void do_other_stuff()
{}
int main()
{
std::future<int> the_answer=std::async(find_the_answer_to_ltuae);
do_other_stuff();
std::cout<<"The answer is "<<the_answer.get()<<std::endl;
//
double a = 1 / 3; a;
if (a = 0.0)
{
int x = 1;
int y = x;
++y;
}
// renc: code from https://bartoszmilewski.com/2011/10/10/async-tasks-in-c11-not-quite-there-yet/
std::cout << "Main thread id: " << std::this_thread::get_id() << std::endl;
std::vector<std::future<void> > futures;
for (int i = 0; i < 20; ++i)
{
// renc: if std::launch enum is not used at the std::async(), implemnent depend.
// if use std::launch::deferred, or std::launch::async, the results are different.
auto fut = std::async([]
{
std::this_thread::sleep_for(std::chrono::seconds(1));
std::cout << std::this_thread::get_id() << " ";
});
futures.push_back(std::move(fut));
}
std::for_each(futures.begin(), futures.end(), [](std::future<void> & fut)
{
fut.wait();
});
std::cout << std::endl;
}