time_point_pass.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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/locks.hpp>
  14. // template <class Mutex> class unique_lock;
  15. // template <class Clock, class Duration>
  16. // unique_lock(mutex_type& m, const chrono::time_point<Clock, Duration>& abs_time);
  17. #define BOOST_THREAD_PROVIDES_GENERIC_SHARED_MUTEX_ON_WIN
  18. #include <boost/thread/lock_types.hpp>
  19. #include <boost/thread/mutex.hpp>
  20. #include <boost/thread/thread.hpp>
  21. #include <boost/detail/lightweight_test.hpp>
  22. #include "../../../../../timming.hpp"
  23. #if defined BOOST_THREAD_USES_CHRONO
  24. boost::timed_mutex m;
  25. typedef boost::chrono::high_resolution_clock Clock;
  26. typedef Clock::time_point time_point;
  27. typedef Clock::duration duration;
  28. typedef boost::chrono::milliseconds ms;
  29. typedef boost::chrono::nanoseconds ns;
  30. time_point t0;
  31. time_point t1;
  32. const ms max_diff(BOOST_THREAD_TEST_TIME_MS);
  33. void f1()
  34. {
  35. t0 = Clock::now();
  36. boost::unique_lock<boost::timed_mutex> lk(m, Clock::now() + ms(750));
  37. BOOST_TEST(lk.owns_lock() == true);
  38. t1 = Clock::now();
  39. }
  40. void f2()
  41. {
  42. t0 = Clock::now();
  43. boost::unique_lock<boost::timed_mutex> lk(m, Clock::now() + ms(250));
  44. BOOST_TEST(lk.owns_lock() == false);
  45. t1 = Clock::now();
  46. ns d = t1 - t0 - ms(250);
  47. BOOST_TEST(d < max_diff);
  48. }
  49. int main()
  50. {
  51. {
  52. m.lock();
  53. boost::thread t(f1);
  54. time_point t2 = Clock::now();
  55. boost::this_thread::sleep_for(ms(250));
  56. time_point t3 = Clock::now();
  57. m.unlock();
  58. t.join();
  59. ns sleep_time = t3 - t2;
  60. ns d_ns = t1 - t0 - sleep_time;
  61. ms d_ms = boost::chrono::duration_cast<boost::chrono::milliseconds>(d_ns);
  62. // BOOST_TEST_GE(d_ms.count(), 0);
  63. BOOST_THREAD_TEST_IT(d_ms, max_diff);
  64. BOOST_THREAD_TEST_IT(d_ns, ns(max_diff));
  65. }
  66. {
  67. m.lock();
  68. boost::thread t(f2);
  69. boost::this_thread::sleep_for(ms(750));
  70. m.unlock();
  71. t.join();
  72. }
  73. return boost::report_errors();
  74. }
  75. #else
  76. #error "Test not applicable: BOOST_THREAD_USES_CHRONO not defined for this platform as not supported"
  77. #endif