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 // TODO(ldionne): This test fails on Ubuntu Focal on our CI nodes (and only there), in 32 bit mode. 12 // UNSUPPORTED: linux && 32bits-on-64bits 13 14 // <condition_variable> 15 16 // class condition_variable; 17 18 // void notify_all(); 19 20 #include <condition_variable> 21 #include <mutex> 22 #include <thread> 23 #include <cassert> 24 25 #include "make_test_thread.h" 26 #include "test_macros.h" 27 28 std::condition_variable cv; 29 std::mutex mut; 30 31 int test0 = 0; 32 int test1 = 0; 33 int test2 = 0; 34 35 void f1() 36 { 37 std::unique_lock<std::mutex> lk(mut); 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 std::unique_lock<std::mutex> lk(mut); 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 = support::make_test_thread(f1); 58 std::thread t2 = support::make_test_thread(f2); 59 std::this_thread::sleep_for(std::chrono::milliseconds(100)); 60 { 61 std::unique_lock<std::mutex>lk(mut); 62 test1 = 1; 63 test2 = 1; 64 } 65 cv.notify_all(); 66 { 67 std::this_thread::sleep_for(std::chrono::milliseconds(100)); 68 std::unique_lock<std::mutex>lk(mut); 69 } 70 t1.join(); 71 t2.join(); 72 assert(test1 == 2); 73 assert(test2 == 2); 74 75 return 0; 76 } 77