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; 14 15 // void notify_one(); 16 17 #include <condition_variable> 18 #include <atomic> 19 #include <mutex> 20 #include <thread> 21 #include <cassert> 22 23 #include "test_macros.h" 24 25 26 std::condition_variable cv; 27 std::mutex mut; 28 29 std::atomic_int test1(0); 30 std::atomic_int test2(0); 31 std::atomic_int ready(2); 32 std::atomic_int which(0); 33 34 void f1() 35 { 36 --ready; 37 std::unique_lock<std::mutex> lk(mut); 38 assert(test1 == 0); 39 while (test1 == 0) 40 cv.wait(lk); 41 which = 1; 42 assert(test1 == 1); 43 test1 = 2; 44 } 45 46 void f2() 47 { 48 --ready; 49 std::unique_lock<std::mutex> lk(mut); 50 assert(test2 == 0); 51 while (test2 == 0) 52 cv.wait(lk); 53 which = 2; 54 assert(test2 == 1); 55 test2 = 2; 56 } 57 58 int main(int, char**) 59 { 60 std::thread t1(f1); 61 std::thread t2(f2); 62 while (ready > 0) 63 std::this_thread::yield(); 64 // In case the threads were preempted right after the atomic decrement but 65 // before cv.wait(), we yield one more time. 66 std::this_thread::yield(); 67 { 68 std::unique_lock<std::mutex>lk(mut); 69 test1 = 1; 70 test2 = 1; 71 ready = 1; 72 } 73 cv.notify_one(); 74 { 75 while (which == 0) 76 std::this_thread::yield(); 77 std::unique_lock<std::mutex>lk(mut); 78 } 79 if (test1 == 2) { 80 assert(test2 == 1); 81 t1.join(); 82 test1 = 0; 83 } else { 84 assert(test1 == 1); 85 assert(test2 == 2); 86 t2.join(); 87 test2 = 0; 88 } 89 which = 0; 90 cv.notify_one(); 91 { 92 while (which == 0) 93 std::this_thread::yield(); 94 std::unique_lock<std::mutex>lk(mut); 95 } 96 if (test1 == 2) { 97 assert(test2 == 0); 98 t1.join(); 99 test1 = 0; 100 } else { 101 assert(test1 == 0); 102 assert(test2 == 2); 103 t2.join(); 104 test2 = 0; 105 } 106 107 return 0; 108 } 109