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 | // |
Howard Hinnant | 412dbeb | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Jonathan Roelofs | b3fcc67 | 2014-09-05 19:45:05 +0000 | [diff] [blame] | 9 | // |
| 10 | // UNSUPPORTED: libcpp-has-no-threads |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 11 | |
| 12 | // <condition_variable> |
| 13 | |
| 14 | // class condition_variable_any; |
| 15 | |
| 16 | // template <class Lock> |
| 17 | // void wait(Lock& lock); |
| 18 | |
| 19 | #include <condition_variable> |
| 20 | #include <mutex> |
| 21 | #include <thread> |
| 22 | #include <cassert> |
| 23 | |
| 24 | std::condition_variable_any cv; |
| 25 | |
| 26 | typedef std::timed_mutex L0; |
| 27 | typedef std::unique_lock<L0> L1; |
| 28 | |
| 29 | L0 m0; |
| 30 | |
| 31 | int test1 = 0; |
| 32 | int test2 = 0; |
| 33 | |
| 34 | void f() |
| 35 | { |
| 36 | L1 lk(m0); |
| 37 | assert(test2 == 0); |
| 38 | test1 = 1; |
| 39 | cv.notify_one(); |
| 40 | while (test2 == 0) |
| 41 | cv.wait(lk); |
| 42 | assert(test2 != 0); |
| 43 | } |
| 44 | |
| 45 | int main() |
| 46 | { |
| 47 | L1 lk(m0); |
| 48 | std::thread t(f); |
| 49 | assert(test1 == 0); |
| 50 | while (test1 == 0) |
| 51 | cv.wait(lk); |
| 52 | assert(test1 != 0); |
| 53 | test2 = 1; |
| 54 | lk.unlock(); |
| 55 | cv.notify_one(); |
| 56 | t.join(); |
| 57 | } |