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