blob: 5b1a60cabf74a9d26b60af995ca22a4a7c8eae74 [file] [log] [blame]
Howard Hinnant27f000e2010-08-30 18:46:21 +00001//===----------------------------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Howard Hinnant412dbeb2010-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 Hinnant27f000e2010-08-30 18:46:21 +00007//
8//===----------------------------------------------------------------------===//
Jonathan Roelofsb3fcc672014-09-05 19:45:05 +00009//
Asiri Rathnayakef520c142015-11-10 11:41:22 +000010// XFAIL: libcpp-no-exceptions
Jonathan Roelofsb3fcc672014-09-05 19:45:05 +000011// UNSUPPORTED: libcpp-has-no-threads
Eric Fiselier10e51f82015-07-28 07:49:15 +000012// UNSUPPORTED: c++98, c++03
Howard Hinnant27f000e2010-08-30 18:46:21 +000013
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
28typedef std::chrono::high_resolution_clock Clock;
29typedef std::chrono::milliseconds ms;
30
31int f0()
32{
33 std::this_thread::sleep_for(ms(200));
34 return 3;
35}
36
37int i = 0;
38
39int& f1()
40{
41 std::this_thread::sleep_for(ms(200));
42 return i;
43}
44
45void f2()
46{
47 std::this_thread::sleep_for(ms(200));
48}
49
Eric Fiselier588e1db2016-04-28 02:00:52 +000050std::unique_ptr<int> f3(int j)
Howard Hinnant27f000e2010-08-30 18:46:21 +000051{
52 std::this_thread::sleep_for(ms(200));
Eric Fiselier588e1db2016-04-28 02:00:52 +000053 return std::unique_ptr<int>(new int(j));
Howard Hinnant27f000e2010-08-30 18:46:21 +000054}
55
56std::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
Eric Fiselier588e1db2016-04-28 02:00:52 +000062void f5(int j)
Marshall Clow4fdb0702014-03-24 22:25:24 +000063{
64 std::this_thread::sleep_for(ms(200));
Eric Fiselier588e1db2016-04-28 02:00:52 +000065 throw j;
Marshall Clow4fdb0702014-03-24 22:25:24 +000066}
67
Howard Hinnant27f000e2010-08-30 18:46:21 +000068int 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 {
Howard Hinnante3120ed2010-11-23 18:33:54 +000095 std::future<int> f = std::async(std::launch::deferred, f0);
Howard Hinnant27f000e2010-08-30 18:46:21 +000096 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 {
Howard Hinnante3120ed2010-11-23 18:33:54 +0000128 std::future<int&> f = std::async(std::launch::deferred, f1);
Howard Hinnant27f000e2010-08-30 18:46:21 +0000129 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 {
Howard Hinnante3120ed2010-11-23 18:33:54 +0000161 std::future<void> f = std::async(std::launch::deferred, f2);
Howard Hinnant27f000e2010-08-30 18:46:21 +0000162 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 }
Howard Hinnantc2bf9e12011-05-18 00:47:00 +0000177
Howard Hinnant27f000e2010-08-30 18:46:21 +0000178 {
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 }
Marshall Clow4fdb0702014-03-24 22:25:24 +0000187
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
Howard Hinnantc2bf9e12011-05-18 00:47:00 +0000200}