Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
Chandler Carruth | 57b08b0 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 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 |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
Jonathan Roelofs | b3fcc67 | 2014-09-05 19:45:05 +0000 | [diff] [blame] | 8 | // |
| 9 | // UNSUPPORTED: libcpp-has-no-threads |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 10 | |
| 11 | // <condition_variable> |
| 12 | |
| 13 | // class condition_variable; |
| 14 | |
| 15 | // void wait(unique_lock<mutex>& lock); |
| 16 | |
| 17 | #include <condition_variable> |
| 18 | #include <mutex> |
| 19 | #include <thread> |
| 20 | #include <cassert> |
| 21 | |
| 22 | std::condition_variable cv; |
| 23 | std::mutex mut; |
| 24 | |
| 25 | int test1 = 0; |
| 26 | int test2 = 0; |
| 27 | |
| 28 | void f() |
| 29 | { |
| 30 | std::unique_lock<std::mutex> lk(mut); |
| 31 | assert(test2 == 0); |
| 32 | test1 = 1; |
| 33 | cv.notify_one(); |
| 34 | while (test2 == 0) |
| 35 | cv.wait(lk); |
| 36 | assert(test2 != 0); |
| 37 | } |
| 38 | |
| 39 | int main() |
| 40 | { |
| 41 | std::unique_lock<std::mutex>lk(mut); |
| 42 | std::thread t(f); |
| 43 | assert(test1 == 0); |
| 44 | while (test1 == 0) |
| 45 | cv.wait(lk); |
| 46 | assert(test1 != 0); |
| 47 | test2 = 1; |
| 48 | lk.unlock(); |
| 49 | cv.notify_one(); |
| 50 | t.join(); |
| 51 | } |