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