forked from jakaspeh/concurrency
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuture.cpp
More file actions
63 lines (40 loc) · 1.42 KB
/
future.cpp
File metadata and controls
63 lines (40 loc) · 1.42 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <string>
#include <chrono>
#include <thread>
#include <future>
void make_break(int milisec)
{
std::this_thread::sleep_for(std::chrono::milliseconds(milisec));
}
int temperature()
{
std::cout << "Husband: Hm, is the weather "
<< "forecast in the newspaper?\n"
<< " Eh, we don't "
<< "have a newspaper at home..." << std::endl;
make_break(2);
std::cout << "Husband: I will look it up on the internet!" << std::endl;
make_break(2);
std::cout << "Husband: Here it is, "
<< "it says tomorrow will be 40." << std::endl;
return 40;
}
int main()
{
std::cout << "Wife: Tomorrow, we are going on a picnic.\n"
<< " What will be the weather...\n"
<< " \"What will be the "
<< "temperature tomorrow?\"" << std::endl;
std::future<int> answer = std::async(temperature);
make_break(2);
std::cout << "Wife: I should pack for tomorrow." << std::endl;
make_break(2);
std::cout << "Wife: Hopefully my husband can figure out the weather soon."
<< std::endl;
int temp = answer.get();
std::cout << "Wife: Finally, tomorrow will be " << temp << "... Em...\n"
<< " \"In which units is the answer?\""
<< std::endl;
return 0;
}