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, c++03
11 // <condition_variable>
13 // class condition_variable;
15 // template <class Rep, class Period>
17 // wait_for(unique_lock<mutex>& lock,
18 // const chrono::duration<Rep, Period>& rel_time);
20 #include <condition_variable>
27 #include "make_test_thread.h"
28 #include "test_macros.h"
30 template <class Function
>
31 std::chrono::microseconds
measure(Function f
) {
32 std::chrono::high_resolution_clock::time_point start
= std::chrono::high_resolution_clock::now();
34 std::chrono::high_resolution_clock::time_point end
= std::chrono::high_resolution_clock::now();
35 return std::chrono::duration_cast
<std::chrono::microseconds
>(end
- start
);
38 int main(int, char**) {
39 // Test unblocking via a call to notify_one() in another thread.
41 // To test this, we set a very long timeout in wait_for() and we wait
42 // again in case we get awoken spuriously. Note that it can actually
43 // happen that we get awoken spuriously and fail to recognize it
44 // (making this test useless), but the likelihood should be small.
46 std::atomic
<bool> ready(false);
47 std::atomic
<bool> likely_spurious(true);
48 auto timeout
= std::chrono::seconds(3600);
49 std::condition_variable cv
;
52 std::thread t1
= support::make_test_thread([&] {
53 std::unique_lock
<std::mutex
> lock(mutex
);
54 auto elapsed
= measure([&] {
57 std::cv_status result
= cv
.wait_for(lock
, timeout
);
58 assert(result
== std::cv_status::no_timeout
);
59 } while (likely_spurious
);
62 // This can technically fail if we have many spurious awakenings, but in practice the
63 // tolerance is so high that it shouldn't be a problem.
64 assert(elapsed
< timeout
);
67 std::thread t2
= support::make_test_thread([&] {
72 // Acquire the same mutex as t1. This blocks the condition variable inside its wait call
73 // so we can notify it while it is waiting.
74 std::unique_lock
<std::mutex
> lock(mutex
);
76 likely_spurious
= false;
84 // Test unblocking via a timeout.
86 // To test this, we create a thread that waits on a condition variable
87 // with a certain timeout, and we never awaken it. To guard against
88 // spurious wakeups, we wait again whenever we are awoken for a reason
89 // other than a timeout.
91 auto timeout
= std::chrono::milliseconds(250);
92 std::condition_variable cv
;
95 std::thread t1
= support::make_test_thread([&] {
96 std::unique_lock
<std::mutex
> lock(mutex
);
97 std::cv_status result
;
99 auto elapsed
= measure([&] { result
= cv
.wait_for(lock
, timeout
); });
100 if (result
== std::cv_status::timeout
)
101 assert(elapsed
>= timeout
);
102 } while (result
!= std::cv_status::timeout
);