Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
Chandler Carruth | 7c3769d | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
Jonathan Roelofs | 8d86b2e | 2014-09-05 19:45:05 +0000 | [diff] [blame] | 8 | // |
| 9 | // UNSUPPORTED: libcpp-has-no-threads |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 10 | |
Eric Fiselier | 8781327 | 2015-08-28 05:46:17 +0000 | [diff] [blame] | 11 | // notify_all_at_thread_exit(...) requires move semantics to transfer the |
| 12 | // unique_lock. |
| 13 | // UNSUPPORTED: c++98, c++03 |
| 14 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 15 | // <condition_variable> |
| 16 | |
| 17 | // void |
| 18 | // notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk); |
| 19 | |
| 20 | #include <condition_variable> |
Howard Hinnant | e6e4d01 | 2010-09-03 21:46:37 +0000 | [diff] [blame] | 21 | #include <mutex> |
| 22 | #include <thread> |
| 23 | #include <chrono> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 24 | #include <cassert> |
| 25 | |
Marshall Clow | b6e011b | 2019-05-31 18:35:30 +0000 | [diff] [blame] | 26 | #include "test_macros.h" |
| 27 | |
Howard Hinnant | e6e4d01 | 2010-09-03 21:46:37 +0000 | [diff] [blame] | 28 | std::condition_variable cv; |
| 29 | std::mutex mut; |
| 30 | |
| 31 | typedef std::chrono::milliseconds ms; |
| 32 | typedef std::chrono::high_resolution_clock Clock; |
| 33 | |
| 34 | void func() |
| 35 | { |
| 36 | std::unique_lock<std::mutex> lk(mut); |
| 37 | std::notify_all_at_thread_exit(cv, std::move(lk)); |
| 38 | std::this_thread::sleep_for(ms(300)); |
| 39 | } |
| 40 | |
JF Bastien | e15dd4e | 2019-02-04 20:31:13 +0000 | [diff] [blame] | 41 | int main(int, char**) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 42 | { |
Howard Hinnant | e6e4d01 | 2010-09-03 21:46:37 +0000 | [diff] [blame] | 43 | std::unique_lock<std::mutex> lk(mut); |
Eric Fiselier | 286e0a4 | 2015-08-18 23:29:59 +0000 | [diff] [blame] | 44 | std::thread t(func); |
Howard Hinnant | e6e4d01 | 2010-09-03 21:46:37 +0000 | [diff] [blame] | 45 | Clock::time_point t0 = Clock::now(); |
| 46 | cv.wait(lk); |
| 47 | Clock::time_point t1 = Clock::now(); |
| 48 | assert(t1-t0 > ms(250)); |
Eric Fiselier | 286e0a4 | 2015-08-18 23:29:59 +0000 | [diff] [blame] | 49 | t.join(); |
JF Bastien | e15dd4e | 2019-02-04 20:31:13 +0000 | [diff] [blame] | 50 | |
| 51 | return 0; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 52 | } |