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