duration_pass.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 shared_lock;
  15. // template <class Rep, class Period>
  16. // shared_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time);
  17. #include <boost/thread/lock_types.hpp>
  18. #include <boost/thread/shared_mutex.hpp>
  19. #include <boost/thread/thread.hpp>
  20. #include <boost/detail/lightweight_test.hpp>
  21. #include <boost/chrono/chrono_io.hpp>
  22. #include "../../../../../timming.hpp"
  23. boost::shared_mutex m;
  24. typedef boost::chrono::high_resolution_clock Clock;
  25. typedef Clock::time_point time_point;
  26. typedef Clock::duration duration;
  27. typedef boost::chrono::milliseconds ms;
  28. typedef boost::chrono::nanoseconds ns;
  29. time_point t0;
  30. time_point t1;
  31. const ms max_diff(BOOST_THREAD_TEST_TIME_MS);
  32. void f1()
  33. {
  34. t0 = Clock::now();
  35. boost::shared_lock<boost::shared_mutex> lk(m, ms(750));
  36. BOOST_TEST(lk.owns_lock() == true);
  37. t1 = Clock::now();
  38. }
  39. void f2()
  40. {
  41. t0 = Clock::now();
  42. boost::shared_lock<boost::shared_mutex> lk(m, ms(250));
  43. BOOST_TEST(lk.owns_lock() == false);
  44. t1 = Clock::now();
  45. ns d = t1 - t0 - ms(250);
  46. BOOST_THREAD_TEST_IT(d, ns(max_diff));
  47. }
  48. int main()
  49. {
  50. {
  51. m.lock();
  52. boost::thread t(f1);
  53. time_point t2 = Clock::now();
  54. boost::this_thread::sleep_for(ms(250));
  55. time_point t3 = Clock::now();
  56. m.unlock();
  57. t.join();
  58. ns sleep_time = t3 - t2;
  59. ns d_ns = t1 - t0 - sleep_time;
  60. ms d_ms = boost::chrono::duration_cast<boost::chrono::milliseconds>(d_ns);
  61. // BOOST_TEST_GE(d_ms.count(), 0);
  62. BOOST_THREAD_TEST_IT(d_ms, max_diff);
  63. BOOST_THREAD_TEST_IT(d_ns, ns(max_diff));
  64. }
  65. {
  66. m.lock();
  67. boost::thread t(f2);
  68. boost::this_thread::sleep_for(ms(750));
  69. m.unlock();
  70. t.join();
  71. }
  72. return boost::report_errors();
  73. }