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