make_unique_lock_mutex_pass.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright (C) 2012 Vicente J. Botet Escriba
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. // <boost/thread/lock_factories.hpp>
  6. // template <class Mutex>
  7. // unique_lock<Mutex> make_unique_lock(Mutex&);
  8. #define BOOST_THREAD_VERSION 4
  9. #include <boost/thread/detail/config.hpp>
  10. #include <boost/thread/lock_factories.hpp>
  11. #include <boost/thread/mutex.hpp>
  12. #include <boost/thread/thread.hpp>
  13. #include <boost/detail/lightweight_test.hpp>
  14. #include "../../../../../timming.hpp"
  15. //#if ! defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  16. boost::mutex m;
  17. #if defined BOOST_THREAD_USES_CHRONO
  18. typedef boost::chrono::high_resolution_clock Clock;
  19. typedef Clock::time_point time_point;
  20. typedef Clock::duration duration;
  21. typedef boost::chrono::milliseconds ms;
  22. typedef boost::chrono::nanoseconds ns;
  23. time_point t0;
  24. time_point t1;
  25. #else
  26. #endif
  27. const ms max_diff(BOOST_THREAD_TEST_TIME_MS);
  28. void f()
  29. {
  30. #if defined BOOST_THREAD_USES_CHRONO
  31. t0 = Clock::now();
  32. {
  33. #if ! defined(BOOST_NO_CXX11_AUTO_DECLARATIONS)
  34. auto
  35. #else
  36. boost::unique_lock<boost::mutex>
  37. #endif
  38. //&&
  39. _ = boost::make_unique_lock(m); (void)_;
  40. t1 = Clock::now();
  41. }
  42. #else
  43. //time_point t0 = Clock::now();
  44. //time_point t1;
  45. {
  46. #if ! defined(BOOST_NO_CXX11_AUTO_DECLARATIONS)
  47. auto
  48. #else
  49. boost::unique_lock<boost::mutex>
  50. #endif
  51. //&&
  52. _ = boost::make_unique_lock(m); (void)_;
  53. //t1 = Clock::now();
  54. }
  55. //ns d = t1 - t0 - ms(250);
  56. //BOOST_TEST(d < max_diff);
  57. #endif
  58. }
  59. int main()
  60. {
  61. m.lock();
  62. boost::thread t(f);
  63. #if defined BOOST_THREAD_USES_CHRONO
  64. time_point t2 = Clock::now();
  65. boost::this_thread::sleep_for(ms(250));
  66. time_point t3 = Clock::now();
  67. #else
  68. #endif
  69. m.unlock();
  70. t.join();
  71. #if defined BOOST_THREAD_USES_CHRONO
  72. ns sleep_time = t3 - t2;
  73. ns d_ns = t1 - t0 - sleep_time;
  74. ms d_ms = boost::chrono::duration_cast<boost::chrono::milliseconds>(d_ns);
  75. // BOOST_TEST_GE(d_ms.count(), 0);
  76. BOOST_THREAD_TEST_IT(d_ms, max_diff);
  77. BOOST_THREAD_TEST_IT(d_ns, ns(max_diff));
  78. #endif
  79. return boost::report_errors();
  80. }
  81. //#else
  82. //int main()
  83. //{
  84. // return boost::report_errors();
  85. //}
  86. //#endif