• 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 // UNSUPPORTED: libcpp-has-no-threads
11 
12 // <condition_variable>
13 
14 // class condition_variable_any;
15 
16 // void notify_all();
17 
18 #include <condition_variable>
19 #include <mutex>
20 #include <thread>
21 #include <cassert>
22 
23 std::condition_variable_any cv;
24 
25 typedef std::timed_mutex L0;
26 typedef std::unique_lock<L0> L1;
27 
28 L0 m0;
29 
30 int test0 = 0;
31 int test1 = 0;
32 int test2 = 0;
33 
f1()34 void f1()
35 {
36     L1 lk(m0);
37     assert(test1 == 0);
38     while (test1 == 0)
39         cv.wait(lk);
40     assert(test1 == 1);
41     test1 = 2;
42 }
43 
f2()44 void f2()
45 {
46     L1 lk(m0);
47     assert(test2 == 0);
48     while (test2 == 0)
49         cv.wait(lk);
50     assert(test2 == 1);
51     test2 = 2;
52 }
53 
main()54 int main()
55 {
56     std::thread t1(f1);
57     std::thread t2(f2);
58     std::this_thread::sleep_for(std::chrono::milliseconds(100));
59     {
60         L1 lk(m0);
61         test1 = 1;
62         test2 = 1;
63     }
64     cv.notify_all();
65     {
66         std::this_thread::sleep_for(std::chrono::milliseconds(100));
67         L1 lk(m0);
68     }
69     t1.join();
70     t2.join();
71     assert(test1 == 2);
72     assert(test2 == 2);
73 }
74