make_nested_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/lock_types.hpp>
  10. #include <boost/thread/strict_lock.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. #ifdef BOOST_THREAD_USES_CHRONO
  16. typedef boost::chrono::high_resolution_clock Clock;
  17. typedef Clock::time_point time_point;
  18. typedef Clock::duration duration;
  19. typedef boost::chrono::milliseconds ms;
  20. typedef boost::chrono::nanoseconds ns;
  21. time_point t0;
  22. time_point t1;
  23. #endif
  24. boost::mutex m;
  25. #if ! defined(BOOST_NO_CXX11_AUTO_DECLARATIONS) && ! defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && ! defined BOOST_THREAD_NO_MAKE_NESTED_STRICT_LOCK && defined BOOST_THREAD_USES_CHRONO
  26. const ms max_diff(BOOST_THREAD_TEST_TIME_MS);
  27. void f()
  28. {
  29. t0 = Clock::now();
  30. boost::unique_lock<boost::mutex> lg(m);
  31. {
  32. const auto&& nlg = boost::make_nested_strict_lock(lg); (void)nlg;
  33. t1 = Clock::now();
  34. }
  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_NESTED_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. }