Home | History | Annotate | Download | only in thread.condition.condvar
      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;
     15 
     16 // void wait(unique_lock<mutex>& lock);
     17 
     18 #include <condition_variable>
     19 #include <mutex>
     20 #include <thread>
     21 #include <cassert>
     22 
     23 std::condition_variable cv;
     24 std::mutex mut;
     25 
     26 int test1 = 0;
     27 int test2 = 0;
     28 
     29 void f()
     30 {
     31     std::unique_lock<std::mutex> lk(mut);
     32     assert(test2 == 0);
     33     test1 = 1;
     34     cv.notify_one();
     35     while (test2 == 0)
     36         cv.wait(lk);
     37     assert(test2 != 0);
     38 }
     39 
     40 int main()
     41 {
     42     std::unique_lock<std::mutex>lk(mut);
     43     std::thread t(f);
     44     assert(test1 == 0);
     45     while (test1 == 0)
     46         cv.wait(lk);
     47     assert(test1 != 0);
     48     test2 = 1;
     49     lk.unlock();
     50     cv.notify_one();
     51     t.join();
     52 }
     53