lock_pass.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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_mutex.hpp>
  14. // class shared_mutex;
  15. // void lock();
  16. #include <boost/thread/shared_mutex.hpp>
  17. #include <boost/thread/thread.hpp>
  18. #include <boost/detail/lightweight_test.hpp>
  19. #include "../../../timming.hpp"
  20. boost::shared_mutex m;
  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. const ms max_diff(BOOST_THREAD_TEST_TIME_MS);
  32. void f()
  33. {
  34. #if defined BOOST_THREAD_USES_CHRONO
  35. t0 = Clock::now();
  36. m.lock();
  37. t1 = Clock::now();
  38. m.unlock();
  39. #else
  40. //time_point t0 = Clock::now();
  41. m.lock();
  42. //time_point t1 = Clock::now();
  43. m.unlock();
  44. //ns d = t1 - t0 - ms(250);
  45. //BOOST_TEST(d < max_diff);
  46. #endif
  47. }
  48. int main()
  49. {
  50. m.lock();
  51. boost::thread t(f);
  52. #if defined BOOST_THREAD_USES_CHRONO
  53. time_point t2 = Clock::now();
  54. boost::this_thread::sleep_for(ms(250));
  55. time_point t3 = Clock::now();
  56. #else
  57. #endif
  58. m.unlock();
  59. t.join();
  60. #if defined BOOST_THREAD_USES_CHRONO
  61. ns sleep_time = t3 - t2;
  62. ns d_ns = t1 - t0 - sleep_time;
  63. ms d_ms = boost::chrono::duration_cast<boost::chrono::milliseconds>(d_ns);
  64. // BOOST_TEST_GE(d_ms.count(), 0);
  65. BOOST_THREAD_TEST_IT(d_ms, max_diff);
  66. BOOST_THREAD_TEST_IT(d_ns, ns(max_diff));
  67. #endif
  68. return boost::report_errors();
  69. }