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 Predicate>
17 //   void wait(unique_lock<mutex>& lock, Predicate pred);
18 
19 #include <condition_variable>
20 #include <mutex>
21 #include <thread>
22 #include <functional>
23 #include <cassert>
24 
25 #include "make_test_thread.h"
26 #include "test_macros.h"
27 
28 std::condition_variable cv;
29 std::mutex mut;
30 
31 int test1 = 0;
32 int test2 = 0;
33 
34 class Pred
35 {
36     int& i_;
37 public:
38     explicit Pred(int& i) : i_(i) {}
39 
40     bool operator()() {return i_ != 0;}
41 };
42 
43 void f()
44 {
45     std::unique_lock<std::mutex> lk(mut);
46     assert(test2 == 0);
47     test1 = 1;
48     cv.notify_one();
49     cv.wait(lk, Pred(test2));
50     assert(test2 != 0);
51 }
52 
53 int main(int, char**)
54 {
55     std::unique_lock<std::mutex>lk(mut);
56     std::thread t = support::make_test_thread(f);
57     assert(test1 == 0);
58     while (test1 == 0)
59         cv.wait(lk);
60     assert(test1 != 0);
61     test2 = 1;
62     lk.unlock();
63     cv.notify_one();
64     t.join();
65 
66   return 0;
67 }
68