Home | History | Annotate | Download | only in thread.condition.condvarany
      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 // UNSUPPORTED: libcpp-has-no-threads
     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 std::condition_variable_any cv;
     26 
     27 typedef std::timed_mutex L0;
     28 typedef std::unique_lock<L0> L1;
     29 
     30 L0 m0;
     31 
     32 int test1 = 0;
     33 int test2 = 0;
     34 
     35 class Pred
     36 {
     37     int& i_;
     38 public:
     39     explicit Pred(int& i) : i_(i) {}
     40 
     41     bool operator()() {return i_ != 0;}
     42 };
     43 
     44 void f()
     45 {
     46     L1 lk(m0);
     47     assert(test2 == 0);
     48     test1 = 1;
     49     cv.notify_one();
     50     cv.wait(lk, Pred(test2));
     51     assert(test2 != 0);
     52 }
     53 
     54 int main()
     55 {
     56     L1 lk(m0);
     57     std::thread t(f);
     58     assert(test1 == 0);
     59     while (test1 == 0)
     60         cv.wait(lk);
     61     assert(test1 != 0);
     62     test2 = 1;
     63     lk.unlock();
     64     cv.notify_one();
     65     t.join();
     66 }
     67