make_strict_lock_pass.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright (C) 2011 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/strict_lock.hpp>
  6. // template <class Lockable>
  7. // strict_lock<Lockable> make_strict_lock(Lockable &);
  8. #define BOOST_THREAD_VERSION 4
  9. #include <boost/thread/strict_lock.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. #ifdef BOOST_THREAD_USES_CHRONO
  15. typedef boost::chrono::high_resolution_clock Clock;
  16. typedef Clock::time_point time_point;
  17. typedef Clock::duration duration;
  18. typedef boost::chrono::milliseconds ms;
  19. typedef boost::chrono::nanoseconds ns;
  20. time_point t0;
  21. time_point t1;
  22. #endif
  23. boost::mutex m;
  24. #if ! defined(BOOST_NO_CXX11_AUTO_DECLARATIONS) && ! defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && ! defined BOOST_THREAD_NO_MAKE_STRICT_LOCK && defined BOOST_THREAD_USES_CHRONO
  25. const ms max_diff(BOOST_THREAD_TEST_TIME_MS);
  26. void f()
  27. {
  28. t0 = Clock::now();
  29. {
  30. const auto&& lg = boost::make_strict_lock(m); (void)lg;
  31. t1 = Clock::now();
  32. }
  33. ns d = t1 - t0 - ms(250);
  34. BOOST_THREAD_TEST_IT(d, ns(max_diff));
  35. }
  36. #endif
  37. int main()
  38. {
  39. #if ! defined(BOOST_NO_CXX11_AUTO_DECLARATIONS) && ! defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && ! defined BOOST_THREAD_NO_MAKE_STRICT_LOCK && defined BOOST_THREAD_USES_CHRONO
  40. {
  41. m.lock();
  42. boost::thread t(f);
  43. time_point t2 = Clock::now();
  44. boost::this_thread::sleep_for(ms(250));
  45. time_point t3 = Clock::now();
  46. m.unlock();
  47. t.join();
  48. ns sleep_time = t3 - t2;
  49. ns d_ns = t1 - t0 - sleep_time;
  50. ms d_ms = boost::chrono::duration_cast<boost::chrono::milliseconds>(d_ns);
  51. // BOOST_TEST_GE(d_ms.count(), 0);
  52. BOOST_THREAD_TEST_IT(d_ms, max_diff);
  53. BOOST_THREAD_TEST_IT(d_ns, ns(max_diff));
  54. }
  55. #endif
  56. return boost::report_errors();
  57. }