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; |
| 15 | |
| 16 | // void wait(unique_lock<mutex>& lock); |
| 17 | |
| 18 | #include <condition_variable> |
| 19 | #include <mutex> |
| 20 | #include <thread> |
| 21 | #include <cassert> |
| 22 | |
| 23 | std::condition_variable cv; |
| 24 | std::mutex mut; |
| 25 | |
| 26 | int test1 = 0; |
| 27 | int test2 = 0; |
| 28 | |
| 29 | void f() |
| 30 | { |
| 31 | std::unique_lock<std::mutex> lk(mut); |
| 32 | assert(test2 == 0); |
| 33 | test1 = 1; |
| 34 | cv.notify_one(); |
| 35 | while (test2 == 0) |
| 36 | cv.wait(lk); |
| 37 | assert(test2 != 0); |
| 38 | } |
| 39 | |
| 40 | int main() |
| 41 | { |
| 42 | std::unique_lock<std::mutex>lk(mut); |
| 43 | std::thread t(f); |
| 44 | assert(test1 == 0); |
| 45 | while (test1 == 0) |
| 46 | cv.wait(lk); |
| 47 | assert(test1 != 0); |
| 48 | test2 = 1; |
| 49 | lk.unlock(); |
| 50 | cv.notify_one(); |
| 51 | t.join(); |
| 52 | } |