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_all();
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_all();
59     {
60         std::this_thread::sleep_for(std::chrono::milliseconds(100));
61         std::unique_lock<std::mutex>lk(mut);
62     }
63     t1.join();
64     t2.join();
65     assert(test1 == 2);
66     assert(test2 == 2);
67 }
68