1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // UNSUPPORTED: libcpp-has-no-threads
10 // UNSUPPORTED: c++98, c++03
11 // ALLOW_RETRIES: 2
12 
13 // <future>
14 
15 // class future<R>
16 
17 // template <class Rep, class Period>
18 //   future_status
19 //   wait_for(const chrono::duration<Rep, Period>& rel_time) const;
20 
21 #include <future>
22 #include <cassert>
23 
24 #include "test_macros.h"
25 
26 typedef std::chrono::milliseconds ms;
27 
28 void func1(std::promise<int> p)
29 {
30     std::this_thread::sleep_for(ms(500));
31     p.set_value(3);
32 }
33 
34 int j = 0;
35 
36 void func3(std::promise<int&> p)
37 {
38     std::this_thread::sleep_for(ms(500));
39     j = 5;
40     p.set_value(j);
41 }
42 
43 void func5(std::promise<void> p)
44 {
45     std::this_thread::sleep_for(ms(500));
46     p.set_value();
47 }
48 
49 template <typename T, typename F>
50 void test(F func) {
51     typedef std::chrono::high_resolution_clock Clock;
52     std::promise<T> p;
53     std::future<T> f = p.get_future();
54     std::thread(func, std::move(p)).detach();
55     assert(f.valid());
56     assert(f.wait_for(ms(300)) == std::future_status::timeout);
57     assert(f.valid());
58     assert(f.wait_for(ms(300)) == std::future_status::ready);
59     assert(f.valid());
60     Clock::time_point t0 = Clock::now();
61     f.wait();
62     Clock::time_point t1 = Clock::now();
63     assert(f.valid());
64     assert(t1-t0 < ms(50));
65 }
66 
67 int main(int, char**)
68 {
69     test<int>(func1);
70     test<int&>(func3);
71     test<void>(func5);
72     return 0;
73 }
74