default_pass.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 strict_lock;
  7. // strict_lock(Mutex &);
  8. #include <boost/thread/strict_lock.hpp>
  9. #include <boost/thread/mutex.hpp>
  10. #include <boost/thread/thread.hpp>
  11. #include <boost/detail/lightweight_test.hpp>
  12. #include "../../../../timming.hpp"
  13. #ifdef BOOST_THREAD_USES_CHRONO
  14. typedef boost::chrono::high_resolution_clock Clock;
  15. typedef Clock::time_point time_point;
  16. typedef Clock::duration duration;
  17. typedef boost::chrono::milliseconds ms;
  18. typedef boost::chrono::nanoseconds ns;
  19. time_point t0;
  20. time_point t1;
  21. #endif
  22. boost::mutex m;
  23. const ms max_diff(BOOST_THREAD_TEST_TIME_MS);
  24. void f()
  25. {
  26. #ifdef BOOST_THREAD_USES_CHRONO
  27. t0 = Clock::now();
  28. {
  29. boost::strict_lock<boost::mutex> lg(m);
  30. t1 = Clock::now();
  31. }
  32. #else
  33. //time_point t0 = Clock::now();
  34. //time_point t1;
  35. {
  36. boost::strict_lock<boost::mutex> lg(m);
  37. //t1 = Clock::now();
  38. }
  39. //ns d = t1 - t0 - ms(250);
  40. //BOOST_TEST(d < max_diff);
  41. #endif
  42. }
  43. int main()
  44. {
  45. m.lock();
  46. boost::thread t(f);
  47. #ifdef BOOST_THREAD_USES_CHRONO
  48. time_point t2 = Clock::now();
  49. boost::this_thread::sleep_for(ms(250));
  50. time_point t3 = Clock::now();
  51. #endif
  52. m.unlock();
  53. t.join();
  54. #if defined BOOST_THREAD_USES_CHRONO
  55. ns sleep_time = t3 - t2;
  56. ns d_ns = t1 - t0 - sleep_time;
  57. ms d_ms = boost::chrono::duration_cast<boost::chrono::milliseconds>(d_ns);
  58. // BOOST_TEST_GE(d_ms.count(), 0);
  59. BOOST_THREAD_TEST_IT(d_ms, max_diff);
  60. BOOST_THREAD_TEST_IT(d_ns, ns(max_diff));
  61. #endif
  62. return boost::report_errors();
  63. }