blob: 16565e7e60558c795ed503a1a5139e8b0403b17d [file] [log] [blame]
Howard Hinnant54da3382010-08-30 18:46:21 +00001//===----------------------------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnant54da3382010-08-30 18:46:21 +00007//
8//===----------------------------------------------------------------------===//
9
10// <future>
11
12// template <class F, class... Args>
13// future<typename result_of<F(Args...)>::type>
14// async(F&& f, Args&&... args);
15
16// template <class F, class... Args>
17// future<typename result_of<F(Args...)>::type>
18// async(launch policy, F&& f, Args&&... args);
19
20#include <future>
21#include <memory>
22#include <cassert>
23
24typedef std::chrono::high_resolution_clock Clock;
25typedef std::chrono::milliseconds ms;
26
27int f0()
28{
29 std::this_thread::sleep_for(ms(200));
30 return 3;
31}
32
33int i = 0;
34
35int& f1()
36{
37 std::this_thread::sleep_for(ms(200));
38 return i;
39}
40
41void f2()
42{
43 std::this_thread::sleep_for(ms(200));
44}
45
46std::unique_ptr<int> f3(int i)
47{
48 std::this_thread::sleep_for(ms(200));
49 return std::unique_ptr<int>(new int(i));
50}
51
52std::unique_ptr<int> f4(std::unique_ptr<int>&& p)
53{
54 std::this_thread::sleep_for(ms(200));
55 return std::move(p);
56}
57
58int main()
59{
60 {
61 std::future<int> f = std::async(f0);
62 std::this_thread::sleep_for(ms(300));
63 Clock::time_point t0 = Clock::now();
64 assert(f.get() == 3);
65 Clock::time_point t1 = Clock::now();
66 assert(t1-t0 < ms(100));
67 }
68 {
69 std::future<int> f = std::async(std::launch::async, f0);
70 std::this_thread::sleep_for(ms(300));
71 Clock::time_point t0 = Clock::now();
72 assert(f.get() == 3);
73 Clock::time_point t1 = Clock::now();
74 assert(t1-t0 < ms(100));
75 }
76 {
77 std::future<int> f = std::async(std::launch::any, f0);
78 std::this_thread::sleep_for(ms(300));
79 Clock::time_point t0 = Clock::now();
80 assert(f.get() == 3);
81 Clock::time_point t1 = Clock::now();
82 assert(t1-t0 < ms(100));
83 }
84 {
85 std::future<int> f = std::async(std::launch::sync, f0);
86 std::this_thread::sleep_for(ms(300));
87 Clock::time_point t0 = Clock::now();
88 assert(f.get() == 3);
89 Clock::time_point t1 = Clock::now();
90 assert(t1-t0 > ms(100));
91 }
92
93 {
94 std::future<int&> f = std::async(f1);
95 std::this_thread::sleep_for(ms(300));
96 Clock::time_point t0 = Clock::now();
97 assert(&f.get() == &i);
98 Clock::time_point t1 = Clock::now();
99 assert(t1-t0 < ms(100));
100 }
101 {
102 std::future<int&> f = std::async(std::launch::async, f1);
103 std::this_thread::sleep_for(ms(300));
104 Clock::time_point t0 = Clock::now();
105 assert(&f.get() == &i);
106 Clock::time_point t1 = Clock::now();
107 assert(t1-t0 < ms(100));
108 }
109 {
110 std::future<int&> f = std::async(std::launch::any, f1);
111 std::this_thread::sleep_for(ms(300));
112 Clock::time_point t0 = Clock::now();
113 assert(&f.get() == &i);
114 Clock::time_point t1 = Clock::now();
115 assert(t1-t0 < ms(100));
116 }
117 {
118 std::future<int&> f = std::async(std::launch::sync, f1);
119 std::this_thread::sleep_for(ms(300));
120 Clock::time_point t0 = Clock::now();
121 assert(&f.get() == &i);
122 Clock::time_point t1 = Clock::now();
123 assert(t1-t0 > ms(100));
124 }
125
126 {
127 std::future<void> f = std::async(f2);
128 std::this_thread::sleep_for(ms(300));
129 Clock::time_point t0 = Clock::now();
130 f.get();
131 Clock::time_point t1 = Clock::now();
132 assert(t1-t0 < ms(100));
133 }
134 {
135 std::future<void> f = std::async(std::launch::async, f2);
136 std::this_thread::sleep_for(ms(300));
137 Clock::time_point t0 = Clock::now();
138 f.get();
139 Clock::time_point t1 = Clock::now();
140 assert(t1-t0 < ms(100));
141 }
142 {
143 std::future<void> f = std::async(std::launch::any, f2);
144 std::this_thread::sleep_for(ms(300));
145 Clock::time_point t0 = Clock::now();
146 f.get();
147 Clock::time_point t1 = Clock::now();
148 assert(t1-t0 < ms(100));
149 }
150 {
151 std::future<void> f = std::async(std::launch::sync, f2);
152 std::this_thread::sleep_for(ms(300));
153 Clock::time_point t0 = Clock::now();
154 f.get();
155 Clock::time_point t1 = Clock::now();
156 assert(t1-t0 > ms(100));
157 }
158
159 {
160 std::future<std::unique_ptr<int>> f = std::async(f3, 3);
161 std::this_thread::sleep_for(ms(300));
162 Clock::time_point t0 = Clock::now();
163 assert(*f.get() == 3);
164 Clock::time_point t1 = Clock::now();
165 assert(t1-t0 < ms(100));
166 }
167
168 {
169 std::future<std::unique_ptr<int>> f =
170 std::async(f4, std::unique_ptr<int>(new int(3)));
171 std::this_thread::sleep_for(ms(300));
172 Clock::time_point t0 = Clock::now();
173 assert(*f.get() == 3);
174 Clock::time_point t1 = Clock::now();
175 assert(t1-t0 < ms(100));
176 }
177}