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 notify_all(); |
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 test0 = 0; |
29 | int test1 = 0; |
30 | int test2 = 0; |
31 | |
32 | void f1() |
33 | { |
34 | std::unique_lock<std::mutex> lk(mut); |
35 | assert(test1 == 0); |
36 | while (test1 == 0) |
37 | cv.wait(lock&: lk); |
38 | assert(test1 == 1); |
39 | test1 = 2; |
40 | } |
41 | |
42 | void f2() |
43 | { |
44 | std::unique_lock<std::mutex> lk(mut); |
45 | assert(test2 == 0); |
46 | while (test2 == 0) |
47 | cv.wait(lock&: lk); |
48 | assert(test2 == 1); |
49 | test2 = 2; |
50 | } |
51 | |
52 | int main(int, char**) |
53 | { |
54 | std::thread t1 = support::make_test_thread(f1); |
55 | std::thread t2 = support::make_test_thread(f2); |
56 | std::this_thread::sleep_for(rtime: std::chrono::milliseconds(100)); |
57 | { |
58 | std::unique_lock<std::mutex>lk(mut); |
59 | test1 = 1; |
60 | test2 = 1; |
61 | } |
62 | cv.notify_all(); |
63 | { |
64 | std::this_thread::sleep_for(rtime: std::chrono::milliseconds(100)); |
65 | std::unique_lock<std::mutex>lk(mut); |
66 | } |
67 | t1.join(); |
68 | t2.join(); |
69 | assert(test1 == 2); |
70 | assert(test2 == 2); |
71 | |
72 | return 0; |
73 | } |
74 |