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 // TODO(ldionne): This test fails on Ubuntu Focal on our CI nodes (and only there), in 32 bit mode. 13 // UNSUPPORTED: linux && 32bits-on-64bits 14 15 // <future> 16 17 // class future<R> 18 19 // void wait() const; 20 21 #include <future> 22 #include <cassert> 23 24 #include "make_test_thread.h" 25 #include "test_macros.h" 26 27 void func1(std::promise<int> p) 28 { 29 std::this_thread::sleep_for(std::chrono::milliseconds(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(std::chrono::milliseconds(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(std::chrono::milliseconds(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 typedef std::chrono::duration<double, std::milli> ms; 52 53 std::promise<T> p; 54 std::future<T> f = p.get_future(); 55 support::make_test_thread(func, std::move(p)).detach(); 56 assert(f.valid()); 57 f.wait(); 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(5)); 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