try_to_lock_pass.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is dual licensed under the MIT and the University of Illinois Open
  6. // Source Licenses. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // Copyright (C) 2011 Vicente J. Botet Escriba
  10. //
  11. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  12. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  13. // <boost/thread/locks.hpp>
  14. // template <class Mutex> class unique_lock;
  15. // unique_lock(mutex_type& m, try_to_lock_t);
  16. #include <boost/thread/lock_types.hpp>
  17. #include <boost/thread/mutex.hpp>
  18. #include <boost/thread/thread.hpp>
  19. #include <boost/detail/lightweight_test.hpp>
  20. #include "../../../../../timming.hpp"
  21. boost::mutex m;
  22. #if defined BOOST_THREAD_USES_CHRONO
  23. typedef boost::chrono::high_resolution_clock Clock;
  24. typedef Clock::time_point time_point;
  25. typedef Clock::duration duration;
  26. typedef boost::chrono::milliseconds ms;
  27. typedef boost::chrono::nanoseconds ns;
  28. time_point t0;
  29. time_point t1;
  30. #else
  31. #endif
  32. const ms max_diff(BOOST_THREAD_TEST_TIME_MS);
  33. void f()
  34. {
  35. #if defined BOOST_THREAD_USES_CHRONO
  36. t0 = Clock::now();
  37. {
  38. boost::unique_lock<boost::mutex> lk(m, boost::try_to_lock);
  39. BOOST_TEST(lk.owns_lock() == false);
  40. }
  41. {
  42. boost::unique_lock<boost::mutex> lk(m, boost::try_to_lock);
  43. BOOST_TEST(lk.owns_lock() == false);
  44. }
  45. {
  46. boost::unique_lock<boost::mutex> lk(m, boost::try_to_lock);
  47. BOOST_TEST(lk.owns_lock() == false);
  48. }
  49. for (;;)
  50. {
  51. boost::unique_lock<boost::mutex> lk(m, boost::try_to_lock);
  52. if (lk.owns_lock()) {
  53. t1 = Clock::now();
  54. break;
  55. }
  56. }
  57. //m.unlock();
  58. #else
  59. // time_point t0 = Clock::now();
  60. // {
  61. // boost::unique_lock<boost::mutex> lk(m, boost::try_to_lock);
  62. // BOOST_TEST(lk.owns_lock() == false);
  63. // }
  64. // {
  65. // boost::unique_lock<boost::mutex> lk(m, boost::try_to_lock);
  66. // BOOST_TEST(lk.owns_lock() == false);
  67. // }
  68. // {
  69. // boost::unique_lock<boost::mutex> lk(m, boost::try_to_lock);
  70. // BOOST_TEST(lk.owns_lock() == false);
  71. // }
  72. for (;;)
  73. {
  74. boost::unique_lock<boost::mutex> lk(m, boost::try_to_lock);
  75. if (lk.owns_lock()) break;
  76. }
  77. //time_point t1 = Clock::now();
  78. //ns d = t1 - t0 - ms(250);
  79. //BOOST_TEST(d < max_diff);
  80. #endif
  81. }
  82. int main()
  83. {
  84. m.lock();
  85. boost::thread t(f);
  86. #if defined BOOST_THREAD_USES_CHRONO
  87. time_point t2 = Clock::now();
  88. boost::this_thread::sleep_for(ms(250));
  89. time_point t3 = Clock::now();
  90. #else
  91. #endif
  92. m.unlock();
  93. t.join();
  94. #if defined BOOST_THREAD_USES_CHRONO
  95. ns sleep_time = t3 - t2;
  96. ns d_ns = t1 - t0 - sleep_time;
  97. ms d_ms = boost::chrono::duration_cast<boost::chrono::milliseconds>(d_ns);
  98. std::cout << "d_ns: " << d_ns.count() << std::endl;
  99. std::cout << "d_ms: " << d_ms.count() << std::endl;
  100. // BOOST_TEST_GE(d_ms.count(), 0);
  101. BOOST_THREAD_TEST_IT(d_ms, max_diff);
  102. BOOST_THREAD_TEST_IT(d_ns, ns(max_diff));
  103. #endif
  104. return boost::report_errors();
  105. }