Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
Howard Hinnant | 5b08a8a | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | // <condition_variable> |
| 11 | |
| 12 | // class condition_variable_any; |
| 13 | |
| 14 | // template <class Lock> |
| 15 | // void wait(Lock& lock); |
| 16 | |
| 17 | #include <condition_variable> |
| 18 | #include <mutex> |
| 19 | #include <thread> |
| 20 | #include <cassert> |
| 21 | |
| 22 | std::condition_variable_any cv; |
| 23 | |
| 24 | typedef std::timed_mutex L0; |
| 25 | typedef std::unique_lock<L0> L1; |
| 26 | |
| 27 | L0 m0; |
| 28 | |
| 29 | int test1 = 0; |
| 30 | int test2 = 0; |
| 31 | |
| 32 | void f() |
| 33 | { |
| 34 | L1 lk(m0); |
| 35 | assert(test2 == 0); |
| 36 | test1 = 1; |
| 37 | cv.notify_one(); |
| 38 | while (test2 == 0) |
| 39 | cv.wait(lk); |
| 40 | assert(test2 != 0); |
| 41 | } |
| 42 | |
| 43 | int main() |
| 44 | { |
| 45 | L1 lk(m0); |
| 46 | std::thread t(f); |
| 47 | assert(test1 == 0); |
| 48 | while (test1 == 0) |
| 49 | cv.wait(lk); |
| 50 | assert(test1 != 0); |
| 51 | test2 = 1; |
| 52 | lk.unlock(); |
| 53 | cv.notify_one(); |
| 54 | t.join(); |
| 55 | } |