Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / thread / thread.condition / thread.condition.condvarany / notify_all.pass.cpp
blob17124908deb3c7656bfca5b2f1aae72df56236a3
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: no-threads
11 // <condition_variable>
13 // class condition_variable_any;
15 // void notify_all();
17 #include <condition_variable>
18 #include <mutex>
19 #include <thread>
20 #include <vector>
21 #include <atomic>
22 #include <cassert>
24 #include "make_test_thread.h"
25 #include "test_macros.h"
27 std::condition_variable_any cv;
29 typedef std::timed_mutex L0;
30 typedef std::unique_lock<L0> L1;
32 L0 m0;
34 const unsigned threadCount = 2;
35 bool pleaseExit = false;
36 std::atomic<unsigned> notReady;
38 void helper() {
39 L1 lk(m0);
40 --notReady;
41 while (pleaseExit == false)
42 cv.wait(lk);
45 int main(int, char**)
47 notReady = threadCount;
48 std::vector<std::thread> threads(threadCount);
49 for (unsigned i = 0; i < threadCount; i++)
50 threads[i] = support::make_test_thread(helper);
52 while (notReady > 0)
53 std::this_thread::yield();
54 // At this point, both threads have had a chance to acquire the lock and are
55 // either waiting on the condition variable or about to wait.
56 L1 lk(m0);
57 pleaseExit = true;
58 // POSIX does not guarantee reliable scheduling if notify_all is called
59 // without the lock being held.
60 cv.notify_all();
62 // The test will hang if not all of the threads were woken.
63 for (unsigned i = 0; i < threadCount; i++)
64 threads[i].join();
66 return 0;