1*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
2*0a6a1f1dSLionel Sambuc //
3*0a6a1f1dSLionel Sambuc //                     The LLVM Compiler Infrastructure
4*0a6a1f1dSLionel Sambuc //
5*0a6a1f1dSLionel Sambuc // This file is dual licensed under the MIT and the University of Illinois Open
6*0a6a1f1dSLionel Sambuc // Source Licenses. See LICENSE.TXT for details.
7*0a6a1f1dSLionel Sambuc //
8*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
9*0a6a1f1dSLionel Sambuc //
10*0a6a1f1dSLionel Sambuc // UNSUPPORTED: libcpp-has-no-threads
11*0a6a1f1dSLionel Sambuc 
12*0a6a1f1dSLionel Sambuc // <future>
13*0a6a1f1dSLionel Sambuc 
14*0a6a1f1dSLionel Sambuc // class future<R>
15*0a6a1f1dSLionel Sambuc 
16*0a6a1f1dSLionel Sambuc // template <class Rep, class Period>
17*0a6a1f1dSLionel Sambuc //   future_status
18*0a6a1f1dSLionel Sambuc //   wait_for(const chrono::duration<Rep, Period>& rel_time) const;
19*0a6a1f1dSLionel Sambuc 
20*0a6a1f1dSLionel Sambuc #include <future>
21*0a6a1f1dSLionel Sambuc #include <cassert>
22*0a6a1f1dSLionel Sambuc 
23*0a6a1f1dSLionel Sambuc typedef std::chrono::milliseconds ms;
24*0a6a1f1dSLionel Sambuc 
func1(std::promise<int> p)25*0a6a1f1dSLionel Sambuc void func1(std::promise<int> p)
26*0a6a1f1dSLionel Sambuc {
27*0a6a1f1dSLionel Sambuc     std::this_thread::sleep_for(ms(500));
28*0a6a1f1dSLionel Sambuc     p.set_value(3);
29*0a6a1f1dSLionel Sambuc }
30*0a6a1f1dSLionel Sambuc 
31*0a6a1f1dSLionel Sambuc int j = 0;
32*0a6a1f1dSLionel Sambuc 
func3(std::promise<int &> p)33*0a6a1f1dSLionel Sambuc void func3(std::promise<int&> p)
34*0a6a1f1dSLionel Sambuc {
35*0a6a1f1dSLionel Sambuc     std::this_thread::sleep_for(ms(500));
36*0a6a1f1dSLionel Sambuc     j = 5;
37*0a6a1f1dSLionel Sambuc     p.set_value(j);
38*0a6a1f1dSLionel Sambuc }
39*0a6a1f1dSLionel Sambuc 
func5(std::promise<void> p)40*0a6a1f1dSLionel Sambuc void func5(std::promise<void> p)
41*0a6a1f1dSLionel Sambuc {
42*0a6a1f1dSLionel Sambuc     std::this_thread::sleep_for(ms(500));
43*0a6a1f1dSLionel Sambuc     p.set_value();
44*0a6a1f1dSLionel Sambuc }
45*0a6a1f1dSLionel Sambuc 
main()46*0a6a1f1dSLionel Sambuc int main()
47*0a6a1f1dSLionel Sambuc {
48*0a6a1f1dSLionel Sambuc     typedef std::chrono::high_resolution_clock Clock;
49*0a6a1f1dSLionel Sambuc     {
50*0a6a1f1dSLionel Sambuc         typedef int T;
51*0a6a1f1dSLionel Sambuc         std::promise<T> p;
52*0a6a1f1dSLionel Sambuc         std::future<T> f = p.get_future();
53*0a6a1f1dSLionel Sambuc         std::thread(func1, std::move(p)).detach();
54*0a6a1f1dSLionel Sambuc         assert(f.valid());
55*0a6a1f1dSLionel Sambuc         assert(f.wait_for(ms(300)) == std::future_status::timeout);
56*0a6a1f1dSLionel Sambuc         assert(f.valid());
57*0a6a1f1dSLionel Sambuc         assert(f.wait_for(ms(300)) == std::future_status::ready);
58*0a6a1f1dSLionel Sambuc         assert(f.valid());
59*0a6a1f1dSLionel Sambuc         Clock::time_point t0 = Clock::now();
60*0a6a1f1dSLionel Sambuc         f.wait();
61*0a6a1f1dSLionel Sambuc         Clock::time_point t1 = Clock::now();
62*0a6a1f1dSLionel Sambuc         assert(f.valid());
63*0a6a1f1dSLionel Sambuc         assert(t1-t0 < ms(50));
64*0a6a1f1dSLionel Sambuc     }
65*0a6a1f1dSLionel Sambuc     {
66*0a6a1f1dSLionel Sambuc         typedef int& T;
67*0a6a1f1dSLionel Sambuc         std::promise<T> p;
68*0a6a1f1dSLionel Sambuc         std::future<T> f = p.get_future();
69*0a6a1f1dSLionel Sambuc         std::thread(func3, std::move(p)).detach();
70*0a6a1f1dSLionel Sambuc         assert(f.valid());
71*0a6a1f1dSLionel Sambuc         assert(f.wait_for(ms(300)) == std::future_status::timeout);
72*0a6a1f1dSLionel Sambuc         assert(f.valid());
73*0a6a1f1dSLionel Sambuc         assert(f.wait_for(ms(300)) == std::future_status::ready);
74*0a6a1f1dSLionel Sambuc         assert(f.valid());
75*0a6a1f1dSLionel Sambuc         Clock::time_point t0 = Clock::now();
76*0a6a1f1dSLionel Sambuc         f.wait();
77*0a6a1f1dSLionel Sambuc         Clock::time_point t1 = Clock::now();
78*0a6a1f1dSLionel Sambuc         assert(f.valid());
79*0a6a1f1dSLionel Sambuc         assert(t1-t0 < ms(50));
80*0a6a1f1dSLionel Sambuc     }
81*0a6a1f1dSLionel Sambuc     {
82*0a6a1f1dSLionel Sambuc         typedef void T;
83*0a6a1f1dSLionel Sambuc         std::promise<T> p;
84*0a6a1f1dSLionel Sambuc         std::future<T> f = p.get_future();
85*0a6a1f1dSLionel Sambuc         std::thread(func5, std::move(p)).detach();
86*0a6a1f1dSLionel Sambuc         assert(f.valid());
87*0a6a1f1dSLionel Sambuc         assert(f.wait_for(ms(300)) == std::future_status::timeout);
88*0a6a1f1dSLionel Sambuc         assert(f.valid());
89*0a6a1f1dSLionel Sambuc         assert(f.wait_for(ms(300)) == std::future_status::ready);
90*0a6a1f1dSLionel Sambuc         assert(f.valid());
91*0a6a1f1dSLionel Sambuc         Clock::time_point t0 = Clock::now();
92*0a6a1f1dSLionel Sambuc         f.wait();
93*0a6a1f1dSLionel Sambuc         Clock::time_point t1 = Clock::now();
94*0a6a1f1dSLionel Sambuc         assert(f.valid());
95*0a6a1f1dSLionel Sambuc         assert(t1-t0 < ms(50));
96*0a6a1f1dSLionel Sambuc     }
97*0a6a1f1dSLionel Sambuc }
98