1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: no-threads
14 // class shared_future<R>
16 // template <class Rep, class Period>
18 // wait_for(const chrono::duration<Rep, Period>& rel_time) const;
24 #include "make_test_thread.h"
25 #include "test_macros.h"
27 typedef std::chrono::milliseconds ms
;
29 static const ms
sleepTime(500);
30 static const ms
waitTime(5000);
32 void func1(std::promise
<int> p
)
34 std::this_thread::sleep_for(sleepTime
);
40 void func3(std::promise
<int&> p
)
42 std::this_thread::sleep_for(sleepTime
);
47 void func5(std::promise
<void> p
)
49 std::this_thread::sleep_for(sleepTime
);
55 typedef std::chrono::high_resolution_clock Clock
;
60 std::shared_future
<T
> f
= p
.get_future();
61 support::make_test_thread(func1
, std::move(p
)).detach();
63 assert(f
.wait_for(ms(1)) == std::future_status::timeout
);
65 assert(f
.wait_for(waitTime
) == std::future_status::ready
);
73 std::shared_future
<T
> f
= p
.get_future();
74 support::make_test_thread(func3
, std::move(p
)).detach();
76 assert(f
.wait_for(ms(1)) == std::future_status::timeout
);
78 assert(f
.wait_for(waitTime
) == std::future_status::ready
);
86 std::shared_future
<T
> f
= p
.get_future();
87 support::make_test_thread(func5
, std::move(p
)).detach();
89 assert(f
.wait_for(ms(1)) == std::future_status::timeout
);
91 assert(f
.wait_for(waitTime
) == std::future_status::ready
);
100 std::shared_future
<T
> f
= p
.get_future();
101 Clock::time_point t0
= Clock::now();
102 support::make_test_thread(func1
, std::move(p
)).detach();
104 assert(f
.wait_for(ms(1)) == std::future_status::timeout
);
107 Clock::time_point t1
= Clock::now();
109 assert(t1
- t0
>= sleepTime
);
110 assert(f
.wait_for(waitTime
) == std::future_status::ready
);
116 std::shared_future
<T
> f
= p
.get_future();
117 Clock::time_point t0
= Clock::now();
118 support::make_test_thread(func3
, std::move(p
)).detach();
120 assert(f
.wait_for(ms(1)) == std::future_status::timeout
);
123 Clock::time_point t1
= Clock::now();
125 assert(t1
- t0
>= sleepTime
);
126 assert(f
.wait_for(waitTime
) == std::future_status::ready
);
132 std::shared_future
<T
> f
= p
.get_future();
133 Clock::time_point t0
= Clock::now();
134 support::make_test_thread(func5
, std::move(p
)).detach();
136 assert(f
.wait_for(ms(1)) == std::future_status::timeout
);
139 Clock::time_point t1
= Clock::now();
141 assert(t1
- t0
>= sleepTime
);
142 assert(f
.wait_for(waitTime
) == std::future_status::ready
);