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 shared_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 int main(int, char**)
49 {
50     typedef std::chrono::high_resolution_clock Clock;
51     {
52         typedef int T;
53         std::promise<T> p;
54         std::shared_future<T> f = p.get_future();
55         std::thread(func1, std::move(p)).detach();
56         assert(f.valid());
57         assert(f.wait_for(ms(300)) == std::future_status::timeout);
58         assert(f.valid());
59         assert(f.wait_for(ms(300)) == std::future_status::ready);
60         assert(f.valid());
61         Clock::time_point t0 = Clock::now();
62         f.wait();
63         Clock::time_point t1 = Clock::now();
64         assert(f.valid());
65         assert(t1-t0 < ms(5));
66     }
67     {
68         typedef int& T;
69         std::promise<T> p;
70         std::shared_future<T> f = p.get_future();
71         std::thread(func3, std::move(p)).detach();
72         assert(f.valid());
73         assert(f.wait_for(ms(300)) == std::future_status::timeout);
74         assert(f.valid());
75         assert(f.wait_for(ms(300)) == std::future_status::ready);
76         assert(f.valid());
77         Clock::time_point t0 = Clock::now();
78         f.wait();
79         Clock::time_point t1 = Clock::now();
80         assert(f.valid());
81         assert(t1-t0 < ms(5));
82     }
83     {
84         typedef void T;
85         std::promise<T> p;
86         std::shared_future<T> f = p.get_future();
87         std::thread(func5, std::move(p)).detach();
88         assert(f.valid());
89         assert(f.wait_for(ms(300)) == std::future_status::timeout);
90         assert(f.valid());
91         assert(f.wait_for(ms(300)) == std::future_status::ready);
92         assert(f.valid());
93         Clock::time_point t0 = Clock::now();
94         f.wait();
95         Clock::time_point t1 = Clock::now();
96         assert(f.valid());
97         assert(t1-t0 < ms(5));
98     }
99 
100   return 0;
101 }
102