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 // FLAKY_TEST. 12 13 // <condition_variable> 14 15 // class condition_variable_any; 16 17 // void notify_one(); 18 19 #include <condition_variable> 20 #include <mutex> 21 #include <thread> 22 #include <cassert> 23 24 std::condition_variable_any cv; 25 26 typedef std::timed_mutex L0; 27 typedef std::unique_lock<L0> L1; 28 29 L0 m0; 30 31 int test0 = 0; 32 int test1 = 0; 33 int test2 = 0; 34 35 void f1() 36 { 37 L1 lk(m0); 38 assert(test1 == 0); 39 while (test1 == 0) 40 cv.wait(lk); 41 assert(test1 == 1); 42 test1 = 2; 43 } 44 45 void f2() 46 { 47 L1 lk(m0); 48 assert(test2 == 0); 49 while (test2 == 0) 50 cv.wait(lk); 51 assert(test2 == 1); 52 test2 = 2; 53 } 54 55 int main(int, char**) 56 { 57 std::thread t1(f1); 58 std::thread t2(f2); 59 std::this_thread::sleep_for(std::chrono::milliseconds(100)); 60 { 61 L1 lk(m0); 62 test1 = 1; 63 test2 = 1; 64 } 65 cv.notify_one(); 66 { 67 std::this_thread::sleep_for(std::chrono::milliseconds(100)); 68 L1 lk(m0); 69 } 70 if (test1 == 2) 71 { 72 t1.join(); 73 test1 = 0; 74 } 75 else if (test2 == 2) 76 { 77 t2.join(); 78 test2 = 0; 79 } 80 else 81 assert(false); 82 cv.notify_one(); 83 { 84 std::this_thread::sleep_for(std::chrono::milliseconds(100)); 85 L1 lk(m0); 86 } 87 if (test1 == 2) 88 { 89 t1.join(); 90 test1 = 0; 91 } 92 else if (test2 == 2) 93 { 94 t2.join(); 95 test2 = 0; 96 } 97 else 98 assert(false); 99 100 return 0; 101 } 102