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 // ALLOW_RETRIES: 2
13 
14 // <future>
15 
16 // class shared_future<R>
17 
18 // template <class Rep, class Period>
19 //   future_status
20 //   wait_for(const chrono::duration<Rep, Period>& rel_time) const;
21 
22 #include <future>
23 #include <cassert>
24 
25 #include "test_macros.h"
26 
27 typedef std::chrono::milliseconds ms;
28 
29 void func1(std::promise<int> p)
30 {
31     std::this_thread::sleep_for(ms(500));
32     p.set_value(3);
33 }
34 
35 int j = 0;
36 
37 void func3(std::promise<int&> p)
38 {
39     std::this_thread::sleep_for(ms(500));
40     j = 5;
41     p.set_value(j);
42 }
43 
44 void func5(std::promise<void> p)
45 {
46     std::this_thread::sleep_for(ms(500));
47     p.set_value();
48 }
49 
50 int main(int, char**)
51 {
52     typedef std::chrono::high_resolution_clock Clock;
53     {
54         typedef int T;
55         std::promise<T> p;
56         std::shared_future<T> f = p.get_future();
57         std::thread(func1, std::move(p)).detach();
58         assert(f.valid());
59         assert(f.wait_for(ms(300)) == std::future_status::timeout);
60         assert(f.valid());
61         assert(f.wait_for(ms(300)) == std::future_status::ready);
62         assert(f.valid());
63         Clock::time_point t0 = Clock::now();
64         f.wait();
65         Clock::time_point t1 = Clock::now();
66         assert(f.valid());
67         assert(t1-t0 < ms(5));
68     }
69     {
70         typedef int& T;
71         std::promise<T> p;
72         std::shared_future<T> f = p.get_future();
73         std::thread(func3, std::move(p)).detach();
74         assert(f.valid());
75         assert(f.wait_for(ms(300)) == std::future_status::timeout);
76         assert(f.valid());
77         assert(f.wait_for(ms(300)) == std::future_status::ready);
78         assert(f.valid());
79         Clock::time_point t0 = Clock::now();
80         f.wait();
81         Clock::time_point t1 = Clock::now();
82         assert(f.valid());
83         assert(t1-t0 < ms(5));
84     }
85     {
86         typedef void T;
87         std::promise<T> p;
88         std::shared_future<T> f = p.get_future();
89         std::thread(func5, std::move(p)).detach();
90         assert(f.valid());
91         assert(f.wait_for(ms(300)) == std::future_status::timeout);
92         assert(f.valid());
93         assert(f.wait_for(ms(300)) == std::future_status::ready);
94         assert(f.valid());
95         Clock::time_point t0 = Clock::now();
96         f.wait();
97         Clock::time_point t1 = Clock::now();
98         assert(f.valid());
99         assert(t1-t0 < ms(5));
100     }
101 
102   return 0;
103 }
104