1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // UNSUPPORTED: libcpp-has-no-threads 11 12 // FLAKY_TEST 13 14 // <condition_variable> 15 16 // class condition_variable_any; 17 18 // void notify_one(); 19 20 #include <condition_variable> 21 #include <mutex> 22 #include <thread> 23 #include <cassert> 24 25 std::condition_variable_any cv; 26 27 typedef std::timed_mutex L0; 28 typedef std::unique_lock<L0> L1; 29 30 L0 m0; 31 32 int test0 = 0; 33 int test1 = 0; 34 int test2 = 0; 35 f1()36void f1() 37 { 38 L1 lk(m0); 39 assert(test1 == 0); 40 while (test1 == 0) 41 cv.wait(lk); 42 assert(test1 == 1); 43 test1 = 2; 44 } 45 f2()46void f2() 47 { 48 L1 lk(m0); 49 assert(test2 == 0); 50 while (test2 == 0) 51 cv.wait(lk); 52 assert(test2 == 1); 53 test2 = 2; 54 } 55 main()56int main() 57 { 58 std::thread t1(f1); 59 std::thread t2(f2); 60 std::this_thread::sleep_for(std::chrono::milliseconds(100)); 61 { 62 L1 lk(m0); 63 test1 = 1; 64 test2 = 1; 65 } 66 cv.notify_one(); 67 { 68 std::this_thread::sleep_for(std::chrono::milliseconds(100)); 69 L1 lk(m0); 70 } 71 if (test1 == 2) 72 { 73 t1.join(); 74 test1 = 0; 75 } 76 else if (test2 == 2) 77 { 78 t2.join(); 79 test2 = 0; 80 } 81 else 82 assert(false); 83 cv.notify_one(); 84 { 85 std::this_thread::sleep_for(std::chrono::milliseconds(100)); 86 L1 lk(m0); 87 } 88 if (test1 == 2) 89 { 90 t1.join(); 91 test1 = 0; 92 } 93 else if (test2 == 2) 94 { 95 t2.join(); 96 test2 = 0; 97 } 98 else 99 assert(false); 100 } 101