Debian Codegraph @

libcxx/test/std/thread/thread.condition/thread.condition.condvar/wait.pass.cpp

raw
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
11 // <condition_variable>
12
13 // class condition_variable;
14
15 // void wait(unique_lock<mutex>& lock);
16
17 #include <condition_variable>
18 #include <mutex>
19 #include <thread>
20 #include <cassert>
21
22 #include "make_test_thread.h"
23 #include "test_macros.h"
24
25 std::condition_variable cv;
26 std::mutex mut;
27
28 int test1 = 0;
29 int test2 = 0;
30
31 void f()
32 {
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);
40 }
41
42 int main(int, char**)
43 {
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();
54
55 return 0;
56 }