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 // const R& shared_future::get();
17 // R& shared_future<R&>::get();
18 // void shared_future<void>::get();
19 
20 #include <future>
21 #include <cassert>
22 
23 #include "test_macros.h"
24 
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 void func2(std::promise<int> p)
32 {
33     std::this_thread::sleep_for(std::chrono::milliseconds(500));
34     p.set_exception(std::make_exception_ptr(3));
35 }
36 
37 int j = 0;
38 
39 void func3(std::promise<int&> p)
40 {
41     std::this_thread::sleep_for(std::chrono::milliseconds(500));
42     j = 5;
43     p.set_value(j);
44 }
45 
46 void func4(std::promise<int&> p)
47 {
48     std::this_thread::sleep_for(std::chrono::milliseconds(500));
49     p.set_exception(std::make_exception_ptr(3.5));
50 }
51 
52 void func5(std::promise<void> p)
53 {
54     std::this_thread::sleep_for(std::chrono::milliseconds(500));
55     p.set_value();
56 }
57 
58 void func6(std::promise<void> p)
59 {
60     std::this_thread::sleep_for(std::chrono::milliseconds(500));
61     p.set_exception(std::make_exception_ptr('c'));
62 }
63 
64 int main(int, char**)
65 {
66     {
67         typedef int T;
68         {
69             std::promise<T> p;
70             std::shared_future<T> f = p.get_future();
71             std::thread(func1, std::move(p)).detach();
72             assert(f.valid());
73             assert(f.get() == 3);
74             assert(f.valid());
75         }
76 #ifndef TEST_HAS_NO_EXCEPTIONS
77         {
78             std::promise<T> p;
79             std::shared_future<T> f = p.get_future();
80             std::thread(func2, std::move(p)).detach();
81             try
82             {
83                 assert(f.valid());
84                 assert(f.get() == 3);
85                 assert(false);
86             }
87             catch (int i)
88             {
89                 assert(i == 3);
90             }
91             assert(f.valid());
92         }
93 #endif
94     }
95     {
96         typedef int& T;
97         {
98             std::promise<T> p;
99             std::shared_future<T> f = p.get_future();
100             std::thread(func3, std::move(p)).detach();
101             assert(f.valid());
102             assert(f.get() == 5);
103             assert(f.valid());
104         }
105 #ifndef TEST_HAS_NO_EXCEPTIONS
106         {
107             std::promise<T> p;
108             std::shared_future<T> f = p.get_future();
109             std::thread(func4, std::move(p)).detach();
110             try
111             {
112                 assert(f.valid());
113                 assert(f.get() == 3);
114                 assert(false);
115             }
116             catch (double i)
117             {
118                 assert(i == 3.5);
119             }
120             assert(f.valid());
121         }
122 #endif
123     }
124     {
125         typedef void T;
126         {
127             std::promise<T> p;
128             std::shared_future<T> f = p.get_future();
129             std::thread(func5, std::move(p)).detach();
130             assert(f.valid());
131             f.get();
132             assert(f.valid());
133         }
134 #ifndef TEST_HAS_NO_EXCEPTIONS
135         {
136             std::promise<T> p;
137             std::shared_future<T> f = p.get_future();
138             std::thread(func6, std::move(p)).detach();
139             try
140             {
141                 assert(f.valid());
142                 f.get();
143                 assert(false);
144             }
145             catch (char i)
146             {
147                 assert(i == 'c');
148             }
149             assert(f.valid());
150         }
151 #endif
152     }
153 
154   return 0;
155 }
156