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 #include "test_macros.h" 25 26 std::condition_variable_any cv; 27 28 typedef std::timed_mutex L0; 29 typedef std::unique_lock<L0> L1; 30 31 L0 m0; 32 33 int test1 = 0; 34 int test2 = 0; 35 36 class Pred 37 { 38 int& i_; 39 public: 40 explicit Pred(int& i) : i_(i) {} 41 42 bool operator()() {return i_ != 0;} 43 }; 44 45 void f() 46 { 47 L1 lk(m0); 48 assert(test2 == 0); 49 test1 = 1; 50 cv.notify_one(); 51 cv.wait(lk, Pred(test2)); 52 assert(test2 != 0); 53 } 54 55 int main(int, char**) 56 { 57 L1 lk(m0); 58 std::thread t(f); 59 assert(test1 == 0); 60 while (test1 == 0) 61 cv.wait(lk); 62 assert(test1 != 0); 63 test2 = 1; 64 lk.unlock(); 65 cv.notify_one(); 66 t.join(); 67 68 return 0; 69 } 70