wait_until_pass.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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) 2013 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/future.hpp>
  14. // class shared_future<R>
  15. // template <class Rep, class Period>
  16. // future_status
  17. // wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
  18. //#define BOOST_THREAD_VERSION 3
  19. #define BOOST_THREAD_VERSION 4
  20. //#define BOOST_THREAD_USES_LOG
  21. #define BOOST_THREAD_USES_LOG_THREAD_ID
  22. #include <boost/thread/detail/log.hpp>
  23. #include <boost/thread/future.hpp>
  24. #include <boost/thread/thread.hpp>
  25. #include <boost/chrono/chrono_io.hpp>
  26. #include <boost/detail/lightweight_test.hpp>
  27. #include "../../../timming.hpp"
  28. #if defined BOOST_THREAD_USES_CHRONO
  29. #ifdef BOOST_MSVC
  30. #pragma warning(disable: 4127) // conditional expression is constant
  31. #endif
  32. typedef boost::chrono::milliseconds ms;
  33. typedef boost::chrono::nanoseconds ns;
  34. namespace boost
  35. {
  36. template <typename OStream>
  37. OStream& operator<<(OStream& os , boost::future_status st )
  38. {
  39. os << underlying_cast<int>(st) << " ";
  40. return os;
  41. }
  42. template <typename T>
  43. struct wrap
  44. {
  45. wrap(T const& v) :
  46. value(v)
  47. {
  48. }
  49. T value;
  50. };
  51. template <typename T>
  52. exception_ptr make_exception_ptr(T v)
  53. {
  54. return copy_exception(wrap<T> (v));
  55. }
  56. }
  57. void func1(boost::promise<int> p)
  58. {
  59. boost::this_thread::sleep_for(ms(500));
  60. p.set_value(3);
  61. }
  62. int j = 0;
  63. void func3(boost::promise<int&> p)
  64. {
  65. boost::this_thread::sleep_for(ms(500));
  66. j = 5;
  67. p.set_value(j);
  68. }
  69. void func5(boost::promise<void> p)
  70. {
  71. boost::this_thread::sleep_for(ms(500));
  72. p.set_value();
  73. }
  74. const ms max_diff(BOOST_THREAD_TEST_TIME_MS);
  75. int main()
  76. {
  77. BOOST_THREAD_LOG << BOOST_THREAD_END_LOG;
  78. {
  79. typedef boost::chrono::high_resolution_clock Clock;
  80. {
  81. typedef int T;
  82. boost::promise<T> p;
  83. boost::shared_future<T> f((p.get_future()));
  84. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  85. boost::thread(func1, boost::move(p)).detach();
  86. #endif
  87. BOOST_TEST(f.valid());
  88. BOOST_TEST_EQ(f.wait_until(Clock::now() + ms(250)) , boost::future_status::timeout);
  89. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  90. #else
  91. func1(boost::move(p));
  92. #endif
  93. BOOST_TEST(f.valid());
  94. BOOST_TEST_EQ(f.wait_until(Clock::now() + ms(750)) , boost::future_status::ready);
  95. BOOST_TEST(f.valid());
  96. Clock::time_point t0 = Clock::now();
  97. f.wait();
  98. Clock::time_point t1 = Clock::now();
  99. BOOST_TEST(f.valid());
  100. ns d = t1 - t0;
  101. BOOST_THREAD_TEST_IT(d, ns(max_diff));
  102. }
  103. {
  104. typedef int& T;
  105. boost::promise<T> p;
  106. boost::shared_future<T> f((p.get_future()));
  107. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  108. boost::thread(func3, boost::move(p)).detach();
  109. #endif
  110. BOOST_TEST(f.valid());
  111. BOOST_TEST_EQ(f.wait_until(Clock::now() + ms(250)) , boost::future_status::timeout);
  112. BOOST_TEST(f.valid());
  113. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  114. #else
  115. func3(boost::move(p));
  116. #endif
  117. BOOST_TEST_EQ(f.wait_until(Clock::now() + ms(750)) , boost::future_status::ready);
  118. BOOST_TEST(f.valid());
  119. Clock::time_point t0 = Clock::now();
  120. f.wait();
  121. Clock::time_point t1 = Clock::now();
  122. BOOST_TEST(f.valid());
  123. ns d = t1 - t0;
  124. BOOST_THREAD_TEST_IT(d, ns(max_diff));
  125. }
  126. {
  127. typedef void T;
  128. boost::promise<T> p;
  129. boost::shared_future<T> f((p.get_future()));
  130. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  131. boost::thread(func5, boost::move(p)).detach();
  132. #endif
  133. BOOST_TEST(f.valid());
  134. BOOST_TEST_EQ(f.wait_until(Clock::now() + ms(250)) , boost::future_status::timeout);
  135. BOOST_TEST(f.valid());
  136. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  137. #else
  138. func5(boost::move(p));
  139. #endif
  140. BOOST_TEST_EQ(f.wait_until(Clock::now() + ms(750)) , boost::future_status::ready);
  141. BOOST_TEST(f.valid());
  142. Clock::time_point t0 = Clock::now();
  143. f.wait();
  144. Clock::time_point t1 = Clock::now();
  145. BOOST_TEST(f.valid());
  146. ns d = t1 - t0;
  147. BOOST_THREAD_TEST_IT(d, ns(max_diff));
  148. }
  149. }
  150. BOOST_THREAD_LOG << BOOST_THREAD_END_LOG;
  151. return boost::report_errors();
  152. }
  153. #else
  154. #error "Test not applicable: BOOST_THREAD_USES_CHRONO not defined for this platform as not supported"
  155. #endif