adopt_lock_pass.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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) 2012 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/shared_lock_guard.hpp>
  14. // template <class Mutex> class shared_lock_guard;
  15. // shared_lock_guard(mutex_type& m, adopt_lock_t);
  16. #include <boost/thread/shared_lock_guard.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. #if defined BOOST_THREAD_USES_CHRONO
  22. typedef boost::chrono::high_resolution_clock Clock;
  23. typedef Clock::time_point time_point;
  24. typedef Clock::duration duration;
  25. typedef boost::chrono::milliseconds ms;
  26. typedef boost::chrono::nanoseconds ns;
  27. time_point t0;
  28. time_point t1;
  29. #else
  30. #endif
  31. boost::shared_mutex m;
  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. m.lock_shared();
  39. boost::shared_lock_guard<boost::shared_mutex> lg(m, boost::adopt_lock);
  40. t1 = Clock::now();
  41. }
  42. #else
  43. //time_point t0 = Clock::now();
  44. //time_point t1;
  45. {
  46. m.lock_shared();
  47. boost::shared_lock_guard<boost::shared_mutex> lg(m, boost::adopt_lock);
  48. //t1 = Clock::now();
  49. }
  50. //ns d = t1 - t0 - ms(250);
  51. //BOOST_TEST(d < max_diff);
  52. #endif
  53. }
  54. int main()
  55. {
  56. m.lock();
  57. boost::thread t(f);
  58. #if defined BOOST_THREAD_USES_CHRONO
  59. time_point t2 = Clock::now();
  60. boost::this_thread::sleep_for(ms(250));
  61. time_point t3 = Clock::now();
  62. #else
  63. #endif
  64. m.unlock();
  65. t.join();
  66. #if defined BOOST_THREAD_USES_CHRONO
  67. ns sleep_time = t3 - t2;
  68. ns d_ns = t1 - t0 - sleep_time;
  69. ms d_ms = boost::chrono::duration_cast<boost::chrono::milliseconds>(d_ns);
  70. // BOOST_TEST_GE(d_ms.count(), 0);
  71. BOOST_THREAD_TEST_IT(d_ms, max_diff);
  72. BOOST_THREAD_TEST_IT(d_ns, ns(max_diff));
  73. #endif
  74. return boost::report_errors();
  75. }