notify_all_at_thread_exit.pass.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. //===----------------------------------------------------------------------===//
  2. //
  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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // UNSUPPORTED: libcpp-has-no-threads
  10. // notify_all_at_thread_exit(...) requires move semantics to transfer the
  11. // unique_lock.
  12. // UNSUPPORTED: c++98, c++03
  13. // <condition_variable>
  14. // void
  15. // notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk);
  16. #include <condition_variable>
  17. #include <mutex>
  18. #include <thread>
  19. #include <chrono>
  20. #include <cassert>
  21. #include "test_macros.h"
  22. std::condition_variable cv;
  23. std::mutex mut;
  24. typedef std::chrono::milliseconds ms;
  25. typedef std::chrono::high_resolution_clock Clock;
  26. void func()
  27. {
  28. std::unique_lock<std::mutex> lk(mut);
  29. std::notify_all_at_thread_exit(cv, std::move(lk));
  30. std::this_thread::sleep_for(ms(300));
  31. }
  32. int main(int, char**)
  33. {
  34. std::unique_lock<std::mutex> lk(mut);
  35. std::thread t(func);
  36. Clock::time_point t0 = Clock::now();
  37. cv.wait(lk);
  38. Clock::time_point t1 = Clock::now();
  39. assert(t1-t0 > ms(250));
  40. t.join();
  41. return 0;
  42. }