blob: 8dd6d630a79c53468f8e8e66a4c21d2b24dc6795 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001//===----------------------------------------------------------------------===//
2//
Chandler Carruth7c3769d2019-01-19 10:56:40 +00003// 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 Hinnantbc8d3f92010-05-11 19:42:16 +00006//
7//===----------------------------------------------------------------------===//
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +00008//
9// UNSUPPORTED: libcpp-has-no-threads
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000010
Eric Fiselier87813272015-08-28 05:46:17 +000011// notify_all_at_thread_exit(...) requires move semantics to transfer the
12// unique_lock.
13// UNSUPPORTED: c++98, c++03
14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000015// <condition_variable>
16
17// void
18// notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk);
19
20#include <condition_variable>
Howard Hinnante6e4d012010-09-03 21:46:37 +000021#include <mutex>
22#include <thread>
23#include <chrono>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000024#include <cassert>
25
Marshall Clowb6e011b2019-05-31 18:35:30 +000026#include "test_macros.h"
27
Howard Hinnante6e4d012010-09-03 21:46:37 +000028std::condition_variable cv;
29std::mutex mut;
30
31typedef std::chrono::milliseconds ms;
32typedef std::chrono::high_resolution_clock Clock;
33
34void 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 Bastiene15dd4e2019-02-04 20:31:13 +000041int main(int, char**)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000042{
Howard Hinnante6e4d012010-09-03 21:46:37 +000043 std::unique_lock<std::mutex> lk(mut);
Eric Fiselier286e0a42015-08-18 23:29:59 +000044 std::thread t(func);
Howard Hinnante6e4d012010-09-03 21:46:37 +000045 Clock::time_point t0 = Clock::now();
46 cv.wait(lk);
47 Clock::time_point t1 = Clock::now();
48 assert(t1-t0 > ms(250));
Eric Fiselier286e0a42015-08-18 23:29:59 +000049 t.join();
JF Bastiene15dd4e2019-02-04 20:31:13 +000050
51 return 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000052}