try_to_lock_pass.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. // shared_lock(mutex_type& m, try_to_lock_t);
  16. #include <boost/thread/lock_types.hpp>
  17. #include <boost/thread/shared_mutex.hpp>
  18. #include <boost/thread/thread.hpp>
  19. #include <boost/detail/lightweight_test.hpp>
  20. #include "../../../../../timming.hpp"
  21. boost::shared_mutex m;
  22. #if defined BOOST_THREAD_USES_CHRONO
  23. typedef boost::chrono::high_resolution_clock Clock;
  24. typedef Clock::time_point time_point;
  25. typedef Clock::duration duration;
  26. typedef boost::chrono::milliseconds ms;
  27. typedef boost::chrono::nanoseconds ns;
  28. time_point t0;
  29. time_point t1;
  30. #else
  31. #endif
  32. const ms max_diff(BOOST_THREAD_TEST_TIME_MS);
  33. void f()
  34. {
  35. #if defined BOOST_THREAD_USES_CHRONO
  36. t0 = Clock::now();
  37. {
  38. boost::shared_lock<boost::shared_mutex> lk(m, boost::try_to_lock);
  39. BOOST_TEST(lk.owns_lock() == false);
  40. }
  41. {
  42. boost::shared_lock<boost::shared_mutex> lk(m, boost::try_to_lock);
  43. BOOST_TEST(lk.owns_lock() == false);
  44. }
  45. {
  46. boost::shared_lock<boost::shared_mutex> lk(m, boost::try_to_lock);
  47. BOOST_TEST(lk.owns_lock() == false);
  48. }
  49. for (;;)
  50. {
  51. boost::shared_lock<boost::shared_mutex> lk(m, boost::try_to_lock);
  52. if (lk.owns_lock()) {
  53. t1 = Clock::now();
  54. break;
  55. }
  56. }
  57. #else
  58. // time_point t0 = Clock::now();
  59. // {
  60. // boost::shared_lock<boost::shared_mutex> lk(m, boost::try_to_lock);
  61. // BOOST_TEST(lk.owns_lock() == false);
  62. // }
  63. // {
  64. // boost::shared_lock<boost::shared_mutex> lk(m, boost::try_to_lock);
  65. // BOOST_TEST(lk.owns_lock() == false);
  66. // }
  67. // {
  68. // boost::shared_lock<boost::shared_mutex> lk(m, boost::try_to_lock);
  69. // BOOST_TEST(lk.owns_lock() == false);
  70. // }
  71. for (;;)
  72. {
  73. boost::shared_lock<boost::shared_mutex> lk(m, boost::try_to_lock);
  74. if (lk.owns_lock()) break;
  75. }
  76. //time_point t1 = Clock::now();
  77. //ns d = t1 - t0 - ms(250);
  78. //BOOST_TEST(d < max_diff);
  79. #endif
  80. }
  81. int main()
  82. {
  83. m.lock();
  84. boost::thread t(f);
  85. #if defined BOOST_THREAD_USES_CHRONO
  86. time_point t2 = Clock::now();
  87. boost::this_thread::sleep_for(ms(250));
  88. time_point t3 = Clock::now();
  89. #else
  90. #endif
  91. m.unlock();
  92. t.join();
  93. #if defined BOOST_THREAD_USES_CHRONO
  94. ns sleep_time = t3 - t2;
  95. ns d_ns = t1 - t0 - sleep_time;
  96. ms d_ms = boost::chrono::duration_cast<boost::chrono::milliseconds>(d_ns);
  97. // BOOST_TEST_GE(d_ms.count(), 0);
  98. BOOST_THREAD_TEST_IT(d_ms, max_diff);
  99. BOOST_THREAD_TEST_IT(d_ns, ns(max_diff));
  100. #endif
  101. return boost::report_errors();
  102. }