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