make_lock_guard_pass.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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/lock_guard.hpp>
  14. // template <class Lockable>
  15. // lock_guard<Lockable> make_lock_guard(Lockable &);
  16. #define BOOST_THREAD_VERSION 4
  17. #include <boost/thread/lock_guard.hpp>
  18. #include <boost/thread/mutex.hpp>
  19. #include <boost/thread/thread.hpp>
  20. #include <boost/detail/lightweight_test.hpp>
  21. #include "../../../../timming.hpp"
  22. #ifdef 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. #endif
  31. boost::mutex m;
  32. #if ! defined(BOOST_NO_CXX11_AUTO_DECLARATIONS) && ! defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && ! defined BOOST_THREAD_NO_MAKE_LOCK_GUARD && defined BOOST_THREAD_USES_CHRONO
  33. const ms max_diff(BOOST_THREAD_TEST_TIME_MS);
  34. void f()
  35. {
  36. t0 = Clock::now();
  37. {
  38. const auto&& lg = boost::make_lock_guard(m); (void)lg;
  39. t1 = Clock::now();
  40. }
  41. }
  42. #endif
  43. int main()
  44. {
  45. #if ! defined(BOOST_NO_CXX11_AUTO_DECLARATIONS) && ! defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && ! defined BOOST_THREAD_NO_MAKE_LOCK_GUARD && defined BOOST_THREAD_USES_CHRONO
  46. {
  47. m.lock();
  48. boost::thread t(f);
  49. time_point t2 = Clock::now();
  50. boost::this_thread::sleep_for(ms(250));
  51. time_point t3 = Clock::now();
  52. m.unlock();
  53. t.join();
  54. ns sleep_time = t3 - t2;
  55. ns d_ns = t1 - t0 - sleep_time;
  56. ms d_ms = boost::chrono::duration_cast<boost::chrono::milliseconds>(d_ns);
  57. // BOOST_TEST_GE(d_ms.count(), 0);
  58. BOOST_THREAD_TEST_IT(d_ms, max_diff);
  59. BOOST_THREAD_TEST_IT(d_ns, ns(max_diff));
  60. }
  61. #endif
  62. return boost::report_errors();
  63. }