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