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 // ~condition_variable();
     17 
     18 #include <condition_variable>
     19 #include <mutex>
     20 #include <thread>
     21 #include <cassert>
     22 
     23 std::condition_variable* cv;
     24 std::mutex m;
     25 typedef std::unique_lock<std::mutex> Lock;
     26 
     27 bool f_ready = false;
     28 bool g_ready = false;
     29 
     30 void f()
     31 {
     32     Lock lk(m);
     33     f_ready = true;
     34     cv->notify_one();
     35     delete cv;
     36 }
     37 
     38 void g()
     39 {
     40     Lock lk(m);
     41     g_ready = true;
     42     cv->notify_one();
     43     while (!f_ready)
     44         cv->wait(lk);
     45 }
     46 
     47 int main()
     48 {
     49     cv = new std::condition_variable;
     50     std::thread th2(g);
     51     Lock lk(m);
     52     while (!g_ready)
     53         cv->wait(lk);
     54     lk.unlock();
     55     std::thread th1(f);
     56     th1.join();
     57     th2.join();
     58 }
     59