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