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