[mlir] Update Ch-2.md (#121379)
[llvm-project.git] / libcxx / test / std / thread / thread.condition / thread.condition.condvar / wait.pass.cpp
blob1a8222b11e9cffc937534ab9e13a33b5ea6a5f5e
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;
15 // void wait(unique_lock<mutex>& lock);
17 #include <condition_variable>
18 #include <mutex>
19 #include <thread>
20 #include <cassert>
22 #include "make_test_thread.h"
23 #include "test_macros.h"
25 std::condition_variable cv;
26 std::mutex mut;
28 int test1 = 0;
29 int test2 = 0;
31 void f()
33 std::unique_lock<std::mutex> lk(mut);
34 assert(test2 == 0);
35 test1 = 1;
36 cv.notify_one();
37 while (test2 == 0)
38 cv.wait(lk);
39 assert(test2 != 0);
42 int main(int, char**)
44 std::unique_lock<std::mutex> lk(mut);
45 std::thread t = support::make_test_thread(f);
46 assert(test1 == 0);
47 while (test1 == 0)
48 cv.wait(lk);
49 assert(test1 != 0);
50 test2 = 1;
51 lk.unlock();
52 cv.notify_one();
53 t.join();
55 return 0;