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