1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // XFAIL: libcpp-no-exceptions
11 // UNSUPPORTED: libcpp-has-no-threads
12 // UNSUPPORTED: c++98, c++03
13 
14 // <future>
15 
16 // class shared_future<R>
17 
18 // const R& shared_future::get();
19 // R& shared_future<R&>::get();
20 // void shared_future<void>::get();
21 
22 #include <future>
23 #include <cassert>
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()
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         {
77             std::promise<T> p;
78             std::shared_future<T> f = p.get_future();
79             std::thread(func2, std::move(p)).detach();
80             try
81             {
82                 assert(f.valid());
83                 assert(f.get() == 3);
84                 assert(false);
85             }
86             catch (int i)
87             {
88                 assert(i == 3);
89             }
90             assert(f.valid());
91         }
92     }
93     {
94         typedef int& T;
95         {
96             std::promise<T> p;
97             std::shared_future<T> f = p.get_future();
98             std::thread(func3, std::move(p)).detach();
99             assert(f.valid());
100             assert(f.get() == 5);
101             assert(f.valid());
102         }
103         {
104             std::promise<T> p;
105             std::shared_future<T> f = p.get_future();
106             std::thread(func4, std::move(p)).detach();
107             try
108             {
109                 assert(f.valid());
110                 assert(f.get() == 3);
111                 assert(false);
112             }
113             catch (double i)
114             {
115                 assert(i == 3.5);
116             }
117             assert(f.valid());
118         }
119     }
120     {
121         typedef void T;
122         {
123             std::promise<T> p;
124             std::shared_future<T> f = p.get_future();
125             std::thread(func5, std::move(p)).detach();
126             assert(f.valid());
127             f.get();
128             assert(f.valid());
129         }
130         {
131             std::promise<T> p;
132             std::shared_future<T> f = p.get_future();
133             std::thread(func6, std::move(p)).detach();
134             try
135             {
136                 assert(f.valid());
137                 f.get();
138                 assert(false);
139             }
140             catch (char i)
141             {
142                 assert(i == 'c');
143             }
144             assert(f.valid());
145         }
146     }
147 }
148