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 // <condition_variable>
     11 
     12 // class condition_variable_any;
     13 
     14 // void notify_all();
     15 
     16 #include <condition_variable>
     17 #include <mutex>
     18 #include <thread>
     19 #include <cassert>
     20 
     21 std::condition_variable_any cv;
     22 
     23 typedef std::timed_mutex L0;
     24 typedef std::unique_lock<L0> L1;
     25 
     26 L0 m0;
     27 
     28 int test0 = 0;
     29 int test1 = 0;
     30 int test2 = 0;
     31 
     32 void f1()
     33 {
     34     L1 lk(m0);
     35     assert(test1 == 0);
     36     while (test1 == 0)
     37         cv.wait(lk);
     38     assert(test1 == 1);
     39     test1 = 2;
     40 }
     41 
     42 void f2()
     43 {
     44     L1 lk(m0);
     45     assert(test2 == 0);
     46     while (test2 == 0)
     47         cv.wait(lk);
     48     assert(test2 == 1);
     49     test2 = 2;
     50 }
     51 
     52 int main()
     53 {
     54     std::thread t1(f1);
     55     std::thread t2(f2);
     56     std::this_thread::sleep_for(std::chrono::milliseconds(100));
     57     {
     58         L1 lk(m0);
     59         test1 = 1;
     60         test2 = 1;
     61     }
     62     cv.notify_all();
     63     {
     64         std::this_thread::sleep_for(std::chrono::milliseconds(100));
     65         L1 lk(m0);
     66     }
     67     t1.join();
     68     t2.join();
     69     assert(test1 == 2);
     70     assert(test2 == 2);
     71 }
     72