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