1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // <condition_variable> 11 12 // class condition_variable_any; 13 14 // template <class Lock, class Predicate> 15 // void wait(Lock& lock, Predicate pred); 16 17 #include <condition_variable> 18 #include <mutex> 19 #include <thread> 20 #include <functional> 21 #include <cassert> 22 23 std::condition_variable_any cv; 24 25 typedef std::timed_mutex L0; 26 typedef std::unique_lock<L0> L1; 27 28 L0 m0; 29 30 int test1 = 0; 31 int test2 = 0; 32 33 class Pred 34 { 35 int& i_; 36 public: Pred(int & i)37 explicit Pred(int& i) : i_(i) {} 38 operator ()()39 bool operator()() {return i_ != 0;} 40 }; 41 f()42void f() 43 { 44 L1 lk(m0); 45 assert(test2 == 0); 46 test1 = 1; 47 cv.notify_one(); 48 cv.wait(lk, Pred(test2)); 49 assert(test2 != 0); 50 } 51 main()52int main() 53 { 54 L1 lk(m0); 55 std::thread t(f); 56 assert(test1 == 0); 57 while (test1 == 0) 58 cv.wait(lk); 59 assert(test1 != 0); 60 test2 = 1; 61 lk.unlock(); 62 cv.notify_one(); 63 t.join(); 64 } 65