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++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 static const ms sleepTime(500); 28 static const ms waitTime(5000); 29 30 void func1(std::promise<int> p) 31 { 32 std::this_thread::sleep_for(sleepTime); 33 p.set_value(3); 34 } 35 36 int j = 0; 37 38 void func3(std::promise<int&> p) 39 { 40 std::this_thread::sleep_for(sleepTime); 41 j = 5; 42 p.set_value(j); 43 } 44 45 void func5(std::promise<void> p) 46 { 47 std::this_thread::sleep_for(sleepTime); 48 p.set_value(); 49 } 50 51 int main(int, char**) 52 { 53 typedef std::chrono::high_resolution_clock Clock; 54 55 { 56 typedef int T; 57 std::promise<T> p; 58 std::shared_future<T> f = p.get_future(); 59 std::thread(func1, std::move(p)).detach(); 60 assert(f.valid()); 61 assert(f.wait_for(ms(1)) == std::future_status::timeout); 62 assert(f.valid()); 63 assert(f.wait_for(waitTime) == std::future_status::ready); 64 assert(f.valid()); 65 f.wait(); 66 assert(f.valid()); 67 } 68 { 69 typedef int& T; 70 std::promise<T> p; 71 std::shared_future<T> f = p.get_future(); 72 std::thread(func3, std::move(p)).detach(); 73 assert(f.valid()); 74 assert(f.wait_for(ms(1)) == std::future_status::timeout); 75 assert(f.valid()); 76 assert(f.wait_for(waitTime) == std::future_status::ready); 77 assert(f.valid()); 78 f.wait(); 79 assert(f.valid()); 80 } 81 { 82 typedef void T; 83 std::promise<T> p; 84 std::shared_future<T> f = p.get_future(); 85 std::thread(func5, std::move(p)).detach(); 86 assert(f.valid()); 87 assert(f.wait_for(ms(1)) == std::future_status::timeout); 88 assert(f.valid()); 89 assert(f.wait_for(waitTime) == std::future_status::ready); 90 assert(f.valid()); 91 f.wait(); 92 assert(f.valid()); 93 } 94 95 { 96 typedef int T; 97 std::promise<T> p; 98 std::shared_future<T> f = p.get_future(); 99 Clock::time_point t0 = Clock::now(); 100 std::thread(func1, std::move(p)).detach(); 101 assert(f.valid()); 102 assert(f.wait_for(ms(1)) == std::future_status::timeout); 103 assert(f.valid()); 104 f.wait(); 105 Clock::time_point t1 = Clock::now(); 106 assert(f.valid()); 107 assert(t1 - t0 >= sleepTime); 108 assert(f.wait_for(waitTime) == std::future_status::ready); 109 assert(f.valid()); 110 } 111 { 112 typedef int& T; 113 std::promise<T> p; 114 std::shared_future<T> f = p.get_future(); 115 Clock::time_point t0 = Clock::now(); 116 std::thread(func3, std::move(p)).detach(); 117 assert(f.valid()); 118 assert(f.wait_for(ms(1)) == std::future_status::timeout); 119 assert(f.valid()); 120 f.wait(); 121 Clock::time_point t1 = Clock::now(); 122 assert(f.valid()); 123 assert(t1 - t0 >= sleepTime); 124 assert(f.wait_for(waitTime) == std::future_status::ready); 125 assert(f.valid()); 126 } 127 { 128 typedef void T; 129 std::promise<T> p; 130 std::shared_future<T> f = p.get_future(); 131 Clock::time_point t0 = Clock::now(); 132 std::thread(func5, std::move(p)).detach(); 133 assert(f.valid()); 134 assert(f.wait_for(ms(1)) == std::future_status::timeout); 135 assert(f.valid()); 136 f.wait(); 137 Clock::time_point t1 = Clock::now(); 138 assert(f.valid()); 139 assert(t1 - t0 >= sleepTime); 140 assert(f.wait_for(waitTime) == std::future_status::ready); 141 assert(f.valid()); 142 } 143 144 return 0; 145 } 146