Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / thread / thread.condition / thread.condition.condvarany / wait_pred.pass.cpp
bloba5e28137bef8c4da9d1671f1ec7ab1fdcffb4ddb
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_any;
16 // template <class Lock, class Predicate>
17 // void wait(Lock& 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_any cv;
30 typedef std::timed_mutex L0;
31 typedef std::unique_lock<L0> L1;
33 L0 m0;
35 int test1 = 0;
36 int test2 = 0;
38 class Pred
40 int& i_;
41 public:
42 explicit Pred(int& i) : i_(i) {}
44 bool operator()() {return i_ != 0;}
47 void f()
49 L1 lk(m0);
50 assert(test2 == 0);
51 test1 = 1;
52 cv.notify_one();
53 cv.wait(lk, Pred(test2));
54 assert(test2 != 0);
57 int main(int, char**)
59 L1 lk(m0);
60 std::thread t = support::make_test_thread(f);
61 assert(test1 == 0);
62 while (test1 == 0)
63 cv.wait(lk);
64 assert(test1 != 0);
65 test2 = 1;
66 lk.unlock();
67 cv.notify_one();
68 t.join();
70 return 0;