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>
     17 //   void wait(Lock& lock);
     18 
     19 #include <condition_variable>
     20 #include <mutex>
     21 #include <thread>
     22 #include <cassert>
     23 
     24 std::condition_variable_any cv;
     25 
     26 typedef std::timed_mutex L0;
     27 typedef std::unique_lock<L0> L1;
     28 
     29 L0 m0;
     30 
     31 int test1 = 0;
     32 int test2 = 0;
     33 
     34 void f()
     35 {
     36     L1 lk(m0);
     37     assert(test2 == 0);
     38     test1 = 1;
     39     cv.notify_one();
     40     while (test2 == 0)
     41         cv.wait(lk);
     42     assert(test2 != 0);
     43 }
     44 
     45 int main()
     46 {
     47     L1 lk(m0);
     48     std::thread t(f);
     49     assert(test1 == 0);
     50     while (test1 == 0)
     51         cv.wait(lk);
     52     assert(test1 != 0);
     53     test2 = 1;
     54     lk.unlock();
     55     cv.notify_one();
     56     t.join();
     57 }
     58