1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // UNSUPPORTED: libcpp-has-no-threads
10 
11 // <condition_variable>
12 
13 // class condition_variable_any;
14 
15 // void notify_all();
16 
17 #include <condition_variable>
18 #include <mutex>
19 #include <thread>
20 #include <vector>
21 #include <atomic>
22 #include <cassert>
23 
24 #include "test_macros.h"
25 
26 std::condition_variable_any cv;
27 
28 typedef std::timed_mutex L0;
29 typedef std::unique_lock<L0> L1;
30 
31 L0 m0;
32 
33 const unsigned threadCount = 2;
34 bool pleaseExit = false;
35 std::atomic<unsigned> notReady;
36 
37 void helper() {
38   L1 lk(m0);
39   --notReady;
40   while (pleaseExit == false)
41     cv.wait(lk);
42 }
43 
44 int main(int, char**)
45 {
46   notReady = threadCount;
47   std::vector<std::thread> threads(threadCount);
48   for (unsigned i = 0; i < threadCount; i++)
49     threads[i] = std::thread(helper);
50   {
51     while (notReady > 0)
52       std::this_thread::yield();
53     // At this point, both threads have had a chance to acquire the lock and are
54     // either waiting on the condition variable or about to wait.
55     L1 lk(m0);
56     pleaseExit = true;
57     // POSIX does not guarantee reliable scheduling if notify_all is called
58     // without the lock being held.
59     cv.notify_all();
60   }
61   // The test will hang if not all of the threads were woken.
62   for (unsigned i = 0; i < threadCount; i++)
63     threads[i].join();
64 
65   return 0;
66 }
67