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: libcpp-has-no-threads 10 11 // <condition_variable> 12 13 // class condition_variable_any; 14 15 // void notify_all(); 16 17 #include <condition_variable> 18 #include <mutex> 19 #include <thread> 20 #include <cassert> 21 22 std::condition_variable_any cv; 23 24 typedef std::timed_mutex L0; 25 typedef std::unique_lock<L0> L1; 26 27 L0 m0; 28 29 int test0 = 0; 30 int test1 = 0; 31 int test2 = 0; 32 33 void f1() 34 { 35 L1 lk(m0); 36 assert(test1 == 0); 37 while (test1 == 0) 38 cv.wait(lk); 39 assert(test1 == 1); 40 test1 = 2; 41 } 42 43 void f2() 44 { 45 L1 lk(m0); 46 assert(test2 == 0); 47 while (test2 == 0) 48 cv.wait(lk); 49 assert(test2 == 1); 50 test2 = 2; 51 } 52 53 int main(int, char**) 54 { 55 std::thread t1(f1); 56 std::thread t2(f2); 57 std::this_thread::sleep_for(std::chrono::milliseconds(100)); 58 { 59 L1 lk(m0); 60 test1 = 1; 61 test2 = 1; 62 } 63 cv.notify_all(); 64 { 65 std::this_thread::sleep_for(std::chrono::milliseconds(100)); 66 L1 lk(m0); 67 } 68 t1.join(); 69 t2.join(); 70 assert(test1 == 2); 71 assert(test2 == 2); 72 73 return 0; 74 } 75