blob: e10d37cf80646c0be60871d61a010760171a7ec9 [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// void wait() const;
17
18#include <future>
19#include <cassert>
20
Howard Hinnant932209b2011-05-17 23:32:48 +000021void func1(std::promise<int> p)
Howard Hinnant7158e5c2010-08-29 14:20:30 +000022{
23 std::this_thread::sleep_for(std::chrono::milliseconds(500));
24 p.set_value(3);
25}
26
27int j = 0;
28
Howard Hinnant932209b2011-05-17 23:32:48 +000029void func3(std::promise<int&> p)
Howard Hinnant7158e5c2010-08-29 14:20:30 +000030{
31 std::this_thread::sleep_for(std::chrono::milliseconds(500));
32 j = 5;
33 p.set_value(j);
34}
35
Howard Hinnant932209b2011-05-17 23:32:48 +000036void func5(std::promise<void> p)
Howard Hinnant7158e5c2010-08-29 14:20:30 +000037{
38 std::this_thread::sleep_for(std::chrono::milliseconds(500));
39 p.set_value();
40}
41
42int main()
43{
44 typedef std::chrono::high_resolution_clock Clock;
45 typedef std::chrono::duration<double, std::milli> ms;
46 {
47 typedef int T;
48 std::promise<T> p;
49 std::future<T> f = p.get_future();
50 std::thread(func1, std::move(p)).detach();
51 assert(f.valid());
52 f.wait();
53 assert(f.valid());
54 Clock::time_point t0 = Clock::now();
55 f.wait();
56 Clock::time_point t1 = Clock::now();
57 assert(f.valid());
58 assert(t1-t0 < ms(5));
59 }
60 {
61 typedef int& T;
62 std::promise<T> p;
63 std::future<T> f = p.get_future();
64 std::thread(func3, std::move(p)).detach();
65 assert(f.valid());
66 f.wait();
67 assert(f.valid());
68 Clock::time_point t0 = Clock::now();
69 f.wait();
70 Clock::time_point t1 = Clock::now();
71 assert(f.valid());
72 assert(t1-t0 < ms(5));
73 }
74 {
75 typedef void T;
76 std::promise<T> p;
77 std::future<T> f = p.get_future();
78 std::thread(func5, std::move(p)).detach();
79 assert(f.valid());
80 f.wait();
81 assert(f.valid());
82 Clock::time_point t0 = Clock::now();
83 f.wait();
84 Clock::time_point t1 = Clock::now();
85 assert(f.valid());
86 assert(t1-t0 < ms(5));
87 }
88}