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