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