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 
12 // <future>
13 
14 // class future<R>
15 
16 // template <class Rep, class Period>
17 //   future_status
18 //   wait_for(const chrono::duration<Rep, Period>& rel_time) const;
19 
20 #include <future>
21 #include <cassert>
22 
23 #include "test_macros.h"
24 
25 typedef std::chrono::milliseconds ms;
26 
27 void func1(std::promise<int> p)
28 {
29     std::this_thread::sleep_for(ms(500));
30     p.set_value(3);
31 }
32 
33 int j = 0;
34 
35 void func3(std::promise<int&> p)
36 {
37     std::this_thread::sleep_for(ms(500));
38     j = 5;
39     p.set_value(j);
40 }
41 
42 void func5(std::promise<void> p)
43 {
44     std::this_thread::sleep_for(ms(500));
45     p.set_value();
46 }
47 
48 template <typename T, typename F>
49 void test(F func) {
50     typedef std::chrono::high_resolution_clock Clock;
51     std::promise<T> p;
52     std::future<T> f = p.get_future();
53     std::thread(func, std::move(p)).detach();
54     assert(f.valid());
55     assert(f.wait_for(ms(300)) == std::future_status::timeout);
56     assert(f.valid());
57     assert(f.wait_for(ms(300)) == std::future_status::ready);
58     assert(f.valid());
59     Clock::time_point t0 = Clock::now();
60     f.wait();
61     Clock::time_point t1 = Clock::now();
62     assert(f.valid());
63     assert(t1-t0 < ms(50));
64 }
65 
66 int main(int, char**)
67 {
68     test<int>(func1);
69     test<int&>(func3);
70     test<void>(func5);
71     return 0;
72 }
73