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_any; 14 15 // template <class Lock, class Predicate> 16 // void wait(Lock& 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_any cv; 25 26 typedef std::timed_mutex L0; 27 typedef std::unique_lock<L0> L1; 28 29 L0 m0; 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 L1 lk(m0); 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 L1 lk(m0); 56 std::thread t(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