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