1 //===----------------------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 // template <class F, class... Args>
13 // future<typename result_of<F(Args...)>::type>
14 // async(F&& f, Args&&... args);
16 // template <class F, class... Args>
17 // future<typename result_of<F(Args...)>::type>
18 // async(launch policy, F&& f, Args&&... args);
24 typedef std::chrono::high_resolution_clock Clock;
25 typedef std::chrono::milliseconds ms;
29 std::this_thread::sleep_for(ms(200));
37 std::this_thread::sleep_for(ms(200));
43 std::this_thread::sleep_for(ms(200));
46 std::unique_ptr<int> f3(int i)
48 std::this_thread::sleep_for(ms(200));
49 return std::unique_ptr<int>(new int(i));
52 std::unique_ptr<int> f4(std::unique_ptr<int>&& p)
54 std::this_thread::sleep_for(ms(200));
61 std::future<int> f = std::async(f0);
62 std::this_thread::sleep_for(ms(300));
63 Clock::time_point t0 = Clock::now();
65 Clock::time_point t1 = Clock::now();
66 assert(t1-t0 < ms(100));
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();
73 Clock::time_point t1 = Clock::now();
74 assert(t1-t0 < ms(100));
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();
81 Clock::time_point t1 = Clock::now();
82 assert(t1-t0 < ms(100));
85 std::future<int> f = std::async(std::launch::deferred, f0);
86 std::this_thread::sleep_for(ms(300));
87 Clock::time_point t0 = Clock::now();
89 Clock::time_point t1 = Clock::now();
90 assert(t1-t0 > ms(100));
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));
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));
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));
118 std::future<int&> f = std::async(std::launch::deferred, 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));
127 std::future<void> f = std::async(f2);
128 std::this_thread::sleep_for(ms(300));
129 Clock::time_point t0 = Clock::now();
131 Clock::time_point t1 = Clock::now();
132 assert(t1-t0 < ms(100));
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();
139 Clock::time_point t1 = Clock::now();
140 assert(t1-t0 < ms(100));
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();
147 Clock::time_point t1 = Clock::now();
148 assert(t1-t0 < ms(100));
151 std::future<void> f = std::async(std::launch::deferred, f2);
152 std::this_thread::sleep_for(ms(300));
153 Clock::time_point t0 = Clock::now();
155 Clock::time_point t1 = Clock::now();
156 assert(t1-t0 > ms(100));
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));
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));