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