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