test_10964.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // Copyright (C) 2015 Vicente Botet
  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. #define BOOST_THREAD_VERSION 4
  6. #include <boost/config.hpp>
  7. #if ! defined BOOST_NO_CXX11_DECLTYPE
  8. #define BOOST_RESULT_OF_USE_DECLTYPE
  9. #endif
  10. #define BOOST_THREAD_PROVIDES_EXECUTORS
  11. #include <boost/thread/future.hpp>
  12. #include <boost/static_assert.hpp>
  13. #include <cassert>
  14. #include <iostream>
  15. #include <boost/thread/executors/basic_thread_pool.hpp>
  16. struct TestCallback
  17. {
  18. typedef boost::future<void> result_type;
  19. result_type operator()(boost::future<void> future) const
  20. {
  21. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  22. assert(future.is_ready());
  23. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  24. future.wait();
  25. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  26. return boost::make_ready_future();
  27. }
  28. result_type operator()(boost::future<boost::future<void> > future) const
  29. {
  30. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  31. assert(future.is_ready());
  32. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  33. assert(future.get().is_ready());
  34. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  35. //boost::future<void> ff = future.get();
  36. return boost::make_ready_future();
  37. }
  38. result_type operator()(boost::shared_future<void> future) const
  39. {
  40. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  41. assert(future.is_ready());
  42. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  43. future.wait();
  44. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  45. return boost::make_ready_future();
  46. }
  47. result_type operator()(boost::shared_future<boost::future<void> > future) const
  48. {
  49. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  50. assert(future.is_ready());
  51. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  52. assert(future.get().is_ready());
  53. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  54. //boost::future<void> ff = future.get();
  55. return boost::make_ready_future();
  56. }
  57. };
  58. void p1()
  59. {
  60. }
  61. int main()
  62. {
  63. const int number_of_tests = 2;
  64. (void)(number_of_tests);
  65. #if ! defined BOOST_NO_CXX11_DECLTYPE && ! defined BOOST_NO_CXX11_AUTO_DECLARATIONS
  66. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  67. {
  68. auto f1 = boost::make_ready_future().then(TestCallback());
  69. BOOST_STATIC_ASSERT(std::is_same<decltype(f1), boost::future<boost::future<void> > >::value);
  70. f1.wait();
  71. }
  72. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  73. for (int i=0; i< number_of_tests; i++)
  74. {
  75. auto f1 = boost::make_ready_future().then(TestCallback());
  76. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  77. BOOST_STATIC_ASSERT(std::is_same<decltype(f1), boost::future<boost::future<void> > >::value);
  78. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  79. auto f2 = f1.unwrap();
  80. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  81. BOOST_STATIC_ASSERT(std::is_same<decltype(f2), boost::future<void> >::value);
  82. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  83. f2.wait();
  84. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  85. }
  86. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  87. for (int i=0; i< number_of_tests; i++)
  88. {
  89. auto f1 = boost::make_ready_future().then(TestCallback());
  90. BOOST_STATIC_ASSERT(std::is_same<decltype(f1), boost::future<boost::future<void> > >::value);
  91. boost::future<void> f2 = f1.get();
  92. }
  93. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  94. {
  95. auto f1 = boost::make_ready_future().then(TestCallback());
  96. BOOST_STATIC_ASSERT(std::is_same<decltype(f1), boost::future<boost::future<void> > >::value);
  97. auto f3 = f1.then(TestCallback());
  98. BOOST_STATIC_ASSERT(std::is_same<decltype(f3), boost::future<boost::future<void> > >::value);
  99. f3.wait();
  100. }
  101. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  102. for (int i=0; i< number_of_tests; i++)
  103. {
  104. auto f1 = boost::make_ready_future().then(TestCallback());
  105. BOOST_STATIC_ASSERT(std::is_same<decltype(f1), boost::future<boost::future<void> > >::value);
  106. auto f2 = f1.unwrap();
  107. BOOST_STATIC_ASSERT(std::is_same<decltype(f2), boost::future<void> >::value);
  108. auto f3 = f2.then(TestCallback());
  109. BOOST_STATIC_ASSERT(std::is_same<decltype(f3), boost::future<boost::future<void> > >::value);
  110. f3.wait();
  111. }
  112. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  113. for (int i=0; i< number_of_tests; i++)
  114. {
  115. boost::make_ready_future().then(
  116. TestCallback()).unwrap().then(TestCallback()).get();
  117. }
  118. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  119. for (int i=0; i< number_of_tests; i++)
  120. {
  121. boost::future<void> f = boost::async(p1);
  122. f.then(
  123. TestCallback()).unwrap().then(TestCallback()).get();
  124. }
  125. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  126. for (int i=0; i< number_of_tests; i++)
  127. {
  128. auto f1 = boost::make_ready_future().then(TestCallback());
  129. BOOST_STATIC_ASSERT(std::is_same<decltype(f1), boost::future<boost::future<void> > >::value);
  130. auto f3 = f1.then(TestCallback());
  131. BOOST_STATIC_ASSERT(std::is_same<decltype(f3), boost::future<boost::future<void> > >::value);
  132. f3.wait();
  133. }
  134. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  135. for (int i=0; i< number_of_tests; i++)
  136. {
  137. boost::basic_thread_pool executor;
  138. auto f1 = boost::make_ready_future().then(executor, TestCallback());
  139. BOOST_STATIC_ASSERT(std::is_same<decltype(f1), boost::future<boost::future<void> > >::value);
  140. auto f3 = f1.then(executor, TestCallback());
  141. BOOST_STATIC_ASSERT(std::is_same<decltype(f3), boost::future<boost::future<void> > >::value);
  142. f3.wait();
  143. }
  144. #if 1
  145. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  146. // fixme
  147. for (int i=0; i< number_of_tests; i++)
  148. {
  149. boost::basic_thread_pool executor(2);
  150. auto f1 = boost::make_ready_future().then(executor, TestCallback());
  151. BOOST_STATIC_ASSERT(std::is_same<decltype(f1), boost::future<boost::future<void> > >::value);
  152. std::cout << __FILE__ << "[" << __LINE__ << "] " << int(f1.valid()) << std::endl;
  153. auto f2 = f1.unwrap();
  154. std::cout << __FILE__ << "[" << __LINE__ << "] " << int(f2.valid()) << std::endl;
  155. BOOST_STATIC_ASSERT(std::is_same<decltype(f2), boost::future<void> >::value);
  156. auto f3 = f2.then(executor, TestCallback());
  157. BOOST_STATIC_ASSERT(std::is_same<decltype(f3), boost::future<boost::future<void> > >::value);
  158. f3.wait();
  159. }
  160. #endif
  161. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  162. for (int i=0; i< number_of_tests; i++)
  163. {
  164. boost::basic_thread_pool executor;
  165. auto f1 = boost::make_ready_future().then(executor, TestCallback());
  166. BOOST_STATIC_ASSERT(std::is_same<decltype(f1), boost::future<boost::future<void> > >::value);
  167. auto f2 = f1.unwrap();
  168. BOOST_STATIC_ASSERT(std::is_same<decltype(f2), boost::future<void> >::value);
  169. auto f3 = f2.then(executor, TestCallback());
  170. BOOST_STATIC_ASSERT(std::is_same<decltype(f3), boost::future<boost::future<void> > >::value);
  171. f3.wait();
  172. }
  173. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  174. #endif
  175. return 0;
  176. }