Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / thread / thread.condition / thread.condition.condvar / wait_pred.pass.cpp
blob15feba55616b00fa3967c6ebe5ebe33295d338e6
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
10 // ALLOW_RETRIES: 2
12 // <condition_variable>
14 // class condition_variable;
16 // template <class Predicate>
17 // void wait(unique_lock<mutex>& lock, Predicate pred);
19 #include <condition_variable>
20 #include <mutex>
21 #include <thread>
22 #include <functional>
23 #include <cassert>
25 #include "make_test_thread.h"
26 #include "test_macros.h"
28 std::condition_variable cv;
29 std::mutex mut;
31 int test1 = 0;
32 int test2 = 0;
34 class Pred
36 int& i_;
37 public:
38 explicit Pred(int& i) : i_(i) {}
40 bool operator()() {return i_ != 0;}
43 void f()
45 std::unique_lock<std::mutex> lk(mut);
46 assert(test2 == 0);
47 test1 = 1;
48 cv.notify_one();
49 cv.wait(lk, Pred(test2));
50 assert(test2 != 0);
53 int main(int, char**)
55 std::unique_lock<std::mutex>lk(mut);
56 std::thread t = support::make_test_thread(f);
57 assert(test1 == 0);
58 while (test1 == 0)
59 cv.wait(lk);
60 assert(test1 != 0);
61 test2 = 1;
62 lk.unlock();
63 cv.notify_one();
64 t.join();
66 return 0;