notify_all_at_thread_exit_pass.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is dual licensed under the MIT and the University of Illinois Open
  6. // Source Licenses. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // Copyright (C) 2011 Vicente J. Botet Escriba
  10. //
  11. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  12. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  13. // <boost/thread/condition_variable.hpp>
  14. // void notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk);
  15. #define BOOST_THREAD_USES_MOVE
  16. #define BOOST_THREAD_VESRION 3
  17. #include <boost/thread/condition_variable.hpp>
  18. #include <boost/thread/mutex.hpp>
  19. #include <boost/thread/locks.hpp>
  20. #include <boost/thread/thread.hpp>
  21. #include <boost/chrono/chrono.hpp>
  22. #include <boost/detail/lightweight_test.hpp>
  23. boost::condition_variable cv;
  24. boost::mutex mut;
  25. typedef boost::chrono::milliseconds ms;
  26. typedef boost::chrono::high_resolution_clock Clock;
  27. void func()
  28. {
  29. boost::unique_lock < boost::mutex > lk(mut);
  30. boost::notify_all_at_thread_exit(cv, boost::move(lk));
  31. boost::this_thread::sleep_for(ms(300));
  32. }
  33. int main()
  34. {
  35. boost::unique_lock < boost::mutex > lk(mut);
  36. boost::thread(func).detach();
  37. Clock::time_point t0 = Clock::now();
  38. cv.wait(lk);
  39. Clock::time_point t1 = Clock::now();
  40. BOOST_TEST(t1 - t0 > ms(250));
  41. return boost::report_errors();
  42. }