1//===----------------------------------------------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// UNSUPPORTED: no-threads
10// ALLOW_RETRIES: 2
11
12// <condition_variable>
13
14// class condition_variable;
15
16// template <class Rep, class Period, class Predicate>
17// bool
18// wait_for(unique_lock<mutex>& lock,
19// const chrono::duration<Rep, Period>& rel_time,
20// Predicate pred);
21
22#include <condition_variable>
23#include <mutex>
24#include <thread>
25#include <chrono>
26#include <cassert>
27
28#include "make_test_thread.h"
29#include "test_macros.h"
30
31class Pred
32{
33 int& i_;
34public:
35 explicit Pred(int& i) : i_(i) {}
36
37 bool operator()() {return i_ != 0;}
38};
39
40std::condition_variable cv;
41std::mutex mut;
42
43int test1 = 0;
44int test2 = 0;
45
46int runs = 0;
47
48void f()
49{
50 typedef std::chrono::system_clock Clock;
51 typedef std::chrono::milliseconds milliseconds;
52 std::unique_lock<std::mutex> lk(mut);
53 assert(test2 == 0);
54 test1 = 1;
55 cv.notify_one();
56 Clock::time_point t0 = Clock::now();
57 bool r = cv.wait_for(lock&: lk, rtime: milliseconds(250), p: Pred(test2));
58 ((void)r); // Prevent unused warning
59 Clock::time_point t1 = Clock::now();
60 if (runs == 0)
61 {
62 assert(t1 - t0 < milliseconds(250));
63 assert(test2 != 0);
64 }
65 else
66 {
67 assert(t1 - t0 - milliseconds(250) < milliseconds(50));
68 assert(test2 == 0);
69 }
70 ++runs;
71}
72
73int main(int, char**)
74{
75 {
76 std::unique_lock<std::mutex>lk(mut);
77 std::thread t = support::make_test_thread(f);
78 assert(test1 == 0);
79 while (test1 == 0)
80 cv.wait(lock&: lk);
81 assert(test1 != 0);
82 test2 = 1;
83 lk.unlock();
84 cv.notify_one();
85 t.join();
86 }
87 test1 = 0;
88 test2 = 0;
89 {
90 std::unique_lock<std::mutex>lk(mut);
91 std::thread t = support::make_test_thread(f);
92 assert(test1 == 0);
93 while (test1 == 0)
94 cv.wait(lock&: lk);
95 assert(test1 != 0);
96 lk.unlock();
97 t.join();
98 }
99
100 return 0;
101}
102

source code of libcxx/test/std/thread/thread.condition/thread.condition.condvar/wait_for_pred.pass.cpp