Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4 | // |
Howard Hinnant | b64f8b0 | 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 | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Jonathan Roelofs | 8d86b2e | 2014-09-05 19:45:05 +0000 | [diff] [blame] | 9 | // |
| 10 | // UNSUPPORTED: libcpp-has-no-threads |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 11 | |
| 12 | // <condition_variable> |
| 13 | |
| 14 | // void |
| 15 | // notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk); |
| 16 | |
| 17 | #include <condition_variable> |
Howard Hinnant | e6e4d01 | 2010-09-03 21:46:37 +0000 | [diff] [blame] | 18 | #include <mutex> |
| 19 | #include <thread> |
| 20 | #include <chrono> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 21 | #include <cassert> |
| 22 | |
Howard Hinnant | e6e4d01 | 2010-09-03 21:46:37 +0000 | [diff] [blame] | 23 | std::condition_variable cv; |
| 24 | std::mutex mut; |
| 25 | |
| 26 | typedef std::chrono::milliseconds ms; |
| 27 | typedef std::chrono::high_resolution_clock Clock; |
| 28 | |
| 29 | void func() |
| 30 | { |
| 31 | std::unique_lock<std::mutex> lk(mut); |
| 32 | std::notify_all_at_thread_exit(cv, std::move(lk)); |
| 33 | std::this_thread::sleep_for(ms(300)); |
| 34 | } |
| 35 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 36 | int main() |
| 37 | { |
Howard Hinnant | e6e4d01 | 2010-09-03 21:46:37 +0000 | [diff] [blame] | 38 | std::unique_lock<std::mutex> lk(mut); |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 39 | std::thread(func).detach(); |
Howard Hinnant | e6e4d01 | 2010-09-03 21:46:37 +0000 | [diff] [blame] | 40 | Clock::time_point t0 = Clock::now(); |
| 41 | cv.wait(lk); |
| 42 | Clock::time_point t1 = Clock::now(); |
| 43 | assert(t1-t0 > ms(250)); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 44 | } |