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 // void notify_one();
     17 
     18 #include <condition_variable>
     19 #include <mutex>
     20 #include <thread>
     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 test0 = 0;
     31 int test1 = 0;
     32 int test2 = 0;
     33 
     34 void f1()
     35 {
     36     L1 lk(m0);
     37     assert(test1 == 0);
     38     while (test1 == 0)
     39         cv.wait(lk);
     40     assert(test1 == 1);
     41     test1 = 2;
     42 }
     43 
     44 void f2()
     45 {
     46     L1 lk(m0);
     47     assert(test2 == 0);
     48     while (test2 == 0)
     49         cv.wait(lk);
     50     assert(test2 == 1);
     51     test2 = 2;
     52 }
     53 
     54 int main()
     55 {
     56     std::thread t1(f1);
     57     std::thread t2(f2);
     58     std::this_thread::sleep_for(std::chrono::milliseconds(100));
     59     {
     60         L1 lk(m0);
     61         test1 = 1;
     62         test2 = 1;
     63     }
     64     cv.notify_one();
     65     {
     66         std::this_thread::sleep_for(std::chrono::milliseconds(100));
     67         L1 lk(m0);
     68     }
     69     if (test1 == 2)
     70     {
     71         t1.join();
     72         test1 = 0;
     73     }
     74     else if (test2 == 2)
     75     {
     76         t2.join();
     77         test2 = 0;
     78     }
     79     else
     80         assert(false);
     81     cv.notify_one();
     82     {
     83         std::this_thread::sleep_for(std::chrono::milliseconds(100));
     84         L1 lk(m0);
     85     }
     86     if (test1 == 2)
     87     {
     88         t1.join();
     89         test1 = 0;
     90     }
     91     else if (test2 == 2)
     92     {
     93         t2.join();
     94         test2 = 0;
     95     }
     96     else
     97         assert(false);
     98 }
     99