make_lock_guard_adopt_lock_pass.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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/lock_guard.hpp>
  14. // template <class Lockable>
  15. // lock_guard<Lockable> make_lock_guard(Lockable &, adopt_lock_t);
  16. #define BOOST_THREAD_VERSION 4
  17. #include <boost/thread/lock_guard.hpp>
  18. #include <boost/thread/mutex.hpp>
  19. #include <boost/thread/thread.hpp>
  20. #include <boost/detail/lightweight_test.hpp>
  21. #include "../../../../timming.hpp"
  22. #ifdef 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. #endif
  31. boost::mutex m;
  32. #if ! defined(BOOST_NO_CXX11_AUTO_DECLARATIONS) && ! defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && ! defined BOOST_THREAD_NO_MAKE_LOCK_GUARD
  33. const ms max_diff(BOOST_THREAD_TEST_TIME_MS);
  34. void f()
  35. {
  36. #ifdef BOOST_THREAD_USES_CHRONO
  37. t0 = Clock::now();
  38. {
  39. m.lock();
  40. auto&& lg = boost::make_lock_guard(m, boost::adopt_lock); (void)lg;
  41. t1 = Clock::now();
  42. }
  43. #else
  44. //time_point t0 = Clock::now();
  45. //time_point t1;
  46. {
  47. m.lock();
  48. auto&& lg = boost::make_lock_guard(m, boost::adopt_lock); (void)lg;
  49. //t1 = Clock::now();
  50. }
  51. //ns d = t1 - t0 - ms(250);
  52. //BOOST_TEST(d < max_diff);
  53. #endif
  54. }
  55. #endif
  56. int main()
  57. {
  58. #if ! defined(BOOST_NO_CXX11_AUTO_DECLARATIONS) && ! defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && ! defined BOOST_THREAD_NO_MAKE_LOCK_GUARD
  59. m.lock();
  60. boost::thread t(f);
  61. #ifdef BOOST_THREAD_USES_CHRONO
  62. time_point t2 = Clock::now();
  63. boost::this_thread::sleep_for(ms(250));
  64. time_point t3 = Clock::now();
  65. #endif
  66. m.unlock();
  67. t.join();
  68. #if defined BOOST_THREAD_USES_CHRONO
  69. ns sleep_time = t3 - t2;
  70. ns d_ns = t1 - t0 - sleep_time;
  71. ms d_ms = boost::chrono::duration_cast<boost::chrono::milliseconds>(d_ns);
  72. // BOOST_TEST_GE(d_ms.count(), 0);
  73. BOOST_THREAD_TEST_IT(d_ms, max_diff);
  74. BOOST_THREAD_TEST_IT(d_ns, ns(max_diff));
  75. #endif
  76. #endif
  77. return boost::report_errors();
  78. }