blob: 0a381d9ca2f0da57ba435a244b63eca436f5684b [file] [log] [blame]
Howard Hinnant7158e5c2010-08-29 14:20:30 +00001//===----------------------------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnant7158e5c2010-08-29 14:20:30 +00007//
8//===----------------------------------------------------------------------===//
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +00009//
10// UNSUPPORTED: libcpp-has-no-threads
Howard Hinnant7158e5c2010-08-29 14:20:30 +000011
12// <future>
13
14// class future<R>
15
16// template <class Rep, class Period>
17// future_status
18// wait_for(const chrono::duration<Rep, Period>& rel_time) const;
19
20#include <future>
21#include <cassert>
22
23typedef std::chrono::milliseconds ms;
24
Howard Hinnant932209b2011-05-17 23:32:48 +000025void func1(std::promise<int> p)
Howard Hinnant7158e5c2010-08-29 14:20:30 +000026{
27 std::this_thread::sleep_for(ms(500));
28 p.set_value(3);
29}
30
31int j = 0;
32
Howard Hinnant932209b2011-05-17 23:32:48 +000033void func3(std::promise<int&> p)
Howard Hinnant7158e5c2010-08-29 14:20:30 +000034{
35 std::this_thread::sleep_for(ms(500));
36 j = 5;
37 p.set_value(j);
38}
39
Howard Hinnant932209b2011-05-17 23:32:48 +000040void func5(std::promise<void> p)
Howard Hinnant7158e5c2010-08-29 14:20:30 +000041{
42 std::this_thread::sleep_for(ms(500));
43 p.set_value();
44}
45
46int main()
47{
48 typedef std::chrono::high_resolution_clock Clock;
49 {
50 typedef int T;
51 std::promise<T> p;
52 std::future<T> f = p.get_future();
53 std::thread(func1, std::move(p)).detach();
54 assert(f.valid());
55 assert(f.wait_for(ms(300)) == std::future_status::timeout);
56 assert(f.valid());
57 assert(f.wait_for(ms(300)) == std::future_status::ready);
58 assert(f.valid());
59 Clock::time_point t0 = Clock::now();
60 f.wait();
61 Clock::time_point t1 = Clock::now();
62 assert(f.valid());
Daniel Dunbar6b875ef2013-02-11 21:04:34 +000063 assert(t1-t0 < ms(50));
Howard Hinnant7158e5c2010-08-29 14:20:30 +000064 }
65 {
66 typedef int& T;
67 std::promise<T> p;
68 std::future<T> f = p.get_future();
69 std::thread(func3, std::move(p)).detach();
70 assert(f.valid());
71 assert(f.wait_for(ms(300)) == std::future_status::timeout);
72 assert(f.valid());
73 assert(f.wait_for(ms(300)) == std::future_status::ready);
74 assert(f.valid());
75 Clock::time_point t0 = Clock::now();
76 f.wait();
77 Clock::time_point t1 = Clock::now();
78 assert(f.valid());
Daniel Dunbar6b875ef2013-02-11 21:04:34 +000079 assert(t1-t0 < ms(50));
Howard Hinnant7158e5c2010-08-29 14:20:30 +000080 }
81 {
82 typedef void T;
83 std::promise<T> p;
84 std::future<T> f = p.get_future();
85 std::thread(func5, std::move(p)).detach();
86 assert(f.valid());
87 assert(f.wait_for(ms(300)) == std::future_status::timeout);
88 assert(f.valid());
89 assert(f.wait_for(ms(300)) == std::future_status::ready);
90 assert(f.valid());
91 Clock::time_point t0 = Clock::now();
92 f.wait();
93 Clock::time_point t1 = Clock::now();
94 assert(f.valid());
Daniel Dunbar6b875ef2013-02-11 21:04:34 +000095 assert(t1-t0 < ms(50));
Howard Hinnant7158e5c2010-08-29 14:20:30 +000096 }
97}