Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame^] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // ÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊThe LLVM Compiler Infrastructure |
| 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; |
| 13 | |
| 14 | // void wait(unique_lock<mutex>& lock); |
| 15 | |
| 16 | #include <condition_variable> |
| 17 | #include <mutex> |
| 18 | #include <thread> |
| 19 | #include <cassert> |
| 20 | |
| 21 | std::condition_variable cv; |
| 22 | std::mutex mut; |
| 23 | |
| 24 | int test1 = 0; |
| 25 | int test2 = 0; |
| 26 | |
| 27 | void f() |
| 28 | { |
| 29 | std::unique_lock<std::mutex> lk(mut); |
| 30 | assert(test2 == 0); |
| 31 | test1 = 1; |
| 32 | cv.notify_one(); |
| 33 | while (test2 == 0) |
| 34 | cv.wait(lk); |
| 35 | assert(test2 != 0); |
| 36 | } |
| 37 | |
| 38 | int main() |
| 39 | { |
| 40 | std::unique_lock<std::mutex>lk(mut); |
| 41 | std::thread t(f); |
| 42 | assert(test1 == 0); |
| 43 | while (test1 == 0) |
| 44 | cv.wait(lk); |
| 45 | assert(test1 != 0); |
| 46 | test2 = 1; |
| 47 | lk.unlock(); |
| 48 | cv.notify_one(); |
| 49 | t.join(); |
| 50 | } |