default_pass.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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/locks.hpp>
  6. // template <class Mutex> class nested_strict_lock;
  7. // nested_strict_lock(Mutex &);
  8. #include <boost/thread/lock_types.hpp>
  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. const ms max_diff(BOOST_THREAD_TEST_TIME_MS);
  25. void f()
  26. {
  27. #ifdef BOOST_THREAD_USES_CHRONO
  28. t0 = Clock::now();
  29. boost::unique_lock<boost::mutex> lg(m);
  30. {
  31. boost::nested_strict_lock<boost::unique_lock<boost::mutex> > nlg(lg);
  32. t1 = Clock::now();
  33. }
  34. #else
  35. //time_point t0 = Clock::now();
  36. //time_point t1;
  37. boost::unique_lock<boost::mutex> lg(m);
  38. {
  39. boost::nested_strict_lock<boost::unique_lock<boost::mutex> > nlg(lg);
  40. //t1 = Clock::now();
  41. }
  42. //ns d = t1 - t0 - ms(250);
  43. //BOOST_TEST(d < max_diff);
  44. #endif
  45. }
  46. int main()
  47. {
  48. {
  49. m.lock();
  50. boost::thread t(f);
  51. #ifdef BOOST_THREAD_USES_CHRONO
  52. time_point t2 = Clock::now();
  53. boost::this_thread::sleep_for(ms(250));
  54. time_point t3 = Clock::now();
  55. #endif
  56. m.unlock();
  57. t.join();
  58. #if defined BOOST_THREAD_USES_CHRONO
  59. ns sleep_time = t3 - t2;
  60. ns d_ns = t1 - t0 - sleep_time;
  61. ms d_ms = boost::chrono::duration_cast<boost::chrono::milliseconds>(d_ns);
  62. // BOOST_TEST_GE(d_ms.count(), 0);
  63. BOOST_THREAD_TEST_IT(d_ms, max_diff);
  64. BOOST_THREAD_TEST_IT(d_ns, ns(max_diff));
  65. #endif
  66. }
  67. return boost::report_errors();
  68. }