mutex_pass.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. // explicit shared_lock(Mutex& m);
  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> ul(m);
  39. t1 = Clock::now();
  40. }
  41. #else
  42. //time_point t0 = Clock::now();
  43. //time_point t1;
  44. {
  45. boost::shared_lock<boost::shared_mutex> ul(m);
  46. //t1 = Clock::now();
  47. }
  48. //ns d = t1 - t0 - ms(250);
  49. //BOOST_TEST(d < max_diff);
  50. #endif
  51. }
  52. int main()
  53. {
  54. m.lock();
  55. boost::thread t(f);
  56. #if defined BOOST_THREAD_USES_CHRONO
  57. time_point t2 = Clock::now();
  58. boost::this_thread::sleep_for(ms(250));
  59. time_point t3 = Clock::now();
  60. #else
  61. #endif
  62. m.unlock();
  63. t.join();
  64. #if defined BOOST_THREAD_USES_CHRONO
  65. ns sleep_time = t3 - t2;
  66. ns d_ns = t1 - t0 - sleep_time;
  67. ms d_ms = boost::chrono::duration_cast<boost::chrono::milliseconds>(d_ns);
  68. // BOOST_TEST_GE(d_ms.count(), 0);
  69. BOOST_THREAD_TEST_IT(d_ms, max_diff);
  70. BOOST_THREAD_TEST_IT(d_ns, ns(max_diff));
  71. #endif
  72. return boost::report_errors();
  73. }