• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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++03
11 
12 // <future>
13 
14 // class shared_future<R>
15 
16 // void wait() const;
17 
18 #include <future>
19 #include <cassert>
20 
21 #include "make_test_thread.h"
22 #include "test_macros.h"
23 
func1(std::promise<int> p)24 void func1(std::promise<int> p)
25 {
26     std::this_thread::sleep_for(std::chrono::milliseconds(500));
27     p.set_value(3);
28 }
29 
30 int j = 0;
31 
func3(std::promise<int &> p)32 void func3(std::promise<int&> p)
33 {
34     std::this_thread::sleep_for(std::chrono::milliseconds(500));
35     j = 5;
36     p.set_value(j);
37 }
38 
func5(std::promise<void> p)39 void func5(std::promise<void> p)
40 {
41     std::this_thread::sleep_for(std::chrono::milliseconds(500));
42     p.set_value();
43 }
44 
main(int,char **)45 int main(int, char**)
46 {
47     typedef std::chrono::high_resolution_clock Clock;
48     typedef std::chrono::duration<double, std::milli> ms;
49     {
50         typedef int T;
51         std::promise<T> p;
52         std::shared_future<T> f = p.get_future();
53         support::make_test_thread(func1, 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     {
64         typedef int& T;
65         std::promise<T> p;
66         std::shared_future<T> f = p.get_future();
67         support::make_test_thread(func3, std::move(p)).detach();
68         assert(f.valid());
69         f.wait();
70         assert(f.valid());
71         Clock::time_point t0 = Clock::now();
72         f.wait();
73         Clock::time_point t1 = Clock::now();
74         assert(f.valid());
75         assert(t1-t0 < ms(5));
76     }
77     {
78         typedef void T;
79         std::promise<T> p;
80         std::shared_future<T> f = p.get_future();
81         support::make_test_thread(func5, std::move(p)).detach();
82         assert(f.valid());
83         f.wait();
84         assert(f.valid());
85         Clock::time_point t0 = Clock::now();
86         f.wait();
87         Clock::time_point t1 = Clock::now();
88         assert(f.valid());
89         assert(t1-t0 < ms(5));
90     }
91 
92   return 0;
93 }
94