mutex_pass.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. // explicit unique_lock(Mutex& m);
  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 <iostream>
  21. #include "../../../../../timming.hpp"
  22. boost::mutex m;
  23. #if defined BOOST_THREAD_USES_CHRONO
  24. typedef boost::chrono::high_resolution_clock Clock;
  25. typedef Clock::time_point time_point;
  26. typedef Clock::duration duration;
  27. typedef boost::chrono::milliseconds ms;
  28. typedef boost::chrono::nanoseconds ns;
  29. time_point t0;
  30. time_point t1;
  31. #else
  32. #endif
  33. const ms max_diff(BOOST_THREAD_TEST_TIME_MS);
  34. void f()
  35. {
  36. #if defined BOOST_THREAD_USES_CHRONO
  37. t0 = Clock::now();
  38. {
  39. boost::unique_lock<boost::mutex> ul(m);
  40. t1 = Clock::now();
  41. }
  42. #else
  43. //time_point t0 = Clock::now();
  44. //time_point t1;
  45. {
  46. boost::unique_lock<boost::mutex> ul(m);
  47. //t1 = Clock::now();
  48. }
  49. //ns d = t1 - t0 - ms(250);
  50. //BOOST_TEST(d < max_diff);
  51. #endif
  52. }
  53. int main()
  54. {
  55. m.lock();
  56. boost::thread t(f);
  57. #if defined BOOST_THREAD_USES_CHRONO
  58. time_point t2 = Clock::now();
  59. boost::this_thread::sleep_for(ms(250));
  60. time_point t3 = Clock::now();
  61. #else
  62. #endif
  63. m.unlock();
  64. t.join();
  65. #if defined BOOST_THREAD_USES_CHRONO
  66. ns sleep_time = t3 - t2;
  67. ns d_ns = t1 - t0 - sleep_time;
  68. ms d_ms = boost::chrono::duration_cast<boost::chrono::milliseconds>(d_ns);
  69. // BOOST_TEST_GE(d_ms.count(), 0);
  70. BOOST_THREAD_TEST_IT(d_ms, max_diff);
  71. BOOST_THREAD_TEST_IT(d_ns, ns(max_diff));
  72. #endif
  73. return boost::report_errors();
  74. }