make_unique_locks_mutex_pass.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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/lock_factories.hpp>
  10. #include <boost/thread/mutex.hpp>
  11. #include <boost/thread/thread.hpp>
  12. #include <boost/detail/lightweight_test.hpp>
  13. #include "../../../../../timming.hpp"
  14. #if ! defined(BOOST_NO_CXX11_AUTO_DECLARATIONS) && ! defined BOOST_THREAD_NO_MAKE_UNIQUE_LOCKS && ! defined BOOST_NO_CXX11_RVALUE_REFERENCES
  15. boost::mutex m1;
  16. boost::mutex m2;
  17. boost::mutex m3;
  18. #if defined BOOST_THREAD_USES_CHRONO
  19. typedef boost::chrono::high_resolution_clock Clock;
  20. typedef Clock::time_point time_point;
  21. typedef Clock::duration duration;
  22. typedef boost::chrono::milliseconds ms;
  23. typedef boost::chrono::nanoseconds ns;
  24. time_point t0;
  25. time_point t1;
  26. #else
  27. #endif
  28. const ms max_diff(BOOST_THREAD_TEST_TIME_MS);
  29. void f()
  30. {
  31. #if defined BOOST_THREAD_USES_CHRONO
  32. t0 = Clock::now();
  33. {
  34. auto&& _ = boost::make_unique_locks(m1,m2,m3); (void)_;
  35. t1 = Clock::now();
  36. }
  37. #else
  38. //time_point t0 = Clock::now();
  39. //time_point t1;
  40. {
  41. auto&& _ = boost::make_unique_locks(m1,m2,m3); (void)_;
  42. //t1 = Clock::now();
  43. }
  44. //ns d = t1 - t0 - ms(250);
  45. //BOOST_TEST(d < max_diff);
  46. #endif
  47. }
  48. int main()
  49. {
  50. m1.lock();
  51. m2.lock();
  52. m3.lock();
  53. boost::thread t(f);
  54. #if defined BOOST_THREAD_USES_CHRONO
  55. time_point t2 = Clock::now();
  56. boost::this_thread::sleep_for(ms(250));
  57. time_point t3 = Clock::now();
  58. #else
  59. #endif
  60. m1.unlock();
  61. m2.unlock();
  62. m3.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. }
  74. #else
  75. int main()
  76. {
  77. return boost::report_errors();
  78. }
  79. #endif