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++98, c++03
11 
12 // <future>
13 
14 // class future<R>
15 
16 // void wait() const;
17 
18 #include <future>
19 #include <cassert>
20 
21 #include "test_macros.h"
22 
23 void func1(std::promise<int> p)
24 {
25     std::this_thread::sleep_for(std::chrono::milliseconds(500));
26     p.set_value(3);
27 }
28 
29 int j = 0;
30 
31 void func3(std::promise<int&> p)
32 {
33     std::this_thread::sleep_for(std::chrono::milliseconds(500));
34     j = 5;
35     p.set_value(j);
36 }
37 
38 void func5(std::promise<void> p)
39 {
40     std::this_thread::sleep_for(std::chrono::milliseconds(500));
41     p.set_value();
42 }
43 
44 template <typename T, typename F>
45 void test(F func) {
46     typedef std::chrono::high_resolution_clock Clock;
47     typedef std::chrono::duration<double, std::milli> ms;
48 
49     std::promise<T> p;
50     std::future<T> f = p.get_future();
51     std::thread(func, std::move(p)).detach();
52     assert(f.valid());
53     f.wait();
54     assert(f.valid());
55     Clock::time_point t0 = Clock::now();
56     f.wait();
57     Clock::time_point t1 = Clock::now();
58     assert(f.valid());
59     assert(t1-t0 < ms(5));
60 }
61 
62 int main(int, char**)
63 {
64     test<int>(func1);
65     test<int&>(func3);
66     test<void>(func5);
67     return 0;
68 }
69