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