executor.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // Copyright (C) 2012-2013 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. #include <boost/config.hpp>
  6. #if ! defined BOOST_NO_CXX11_DECLTYPE
  7. #define BOOST_RESULT_OF_USE_DECLTYPE
  8. #endif
  9. #include <iostream>
  10. #define BOOST_THREAD_VERSION 4
  11. #define BOOST_THREAD_PROVIDES_EXECUTORS
  12. //#define BOOST_THREAD_USES_LOG
  13. #define BOOST_THREAD_USES_LOG_THREAD_ID
  14. #define BOOST_THREAD_QUEUE_DEPRECATE_OLD
  15. #include <boost/thread/caller_context.hpp>
  16. #include <boost/thread/executors/basic_thread_pool.hpp>
  17. #include <boost/thread/executors/loop_executor.hpp>
  18. #include <boost/thread/executors/serial_executor.hpp>
  19. #include <boost/thread/executors/inline_executor.hpp>
  20. #include <boost/thread/executors/thread_executor.hpp>
  21. #include <boost/thread/executors/executor.hpp>
  22. #include <boost/thread/executors/executor_adaptor.hpp>
  23. #include <boost/thread/executor.hpp>
  24. #include <boost/thread/future.hpp>
  25. #include <boost/assert.hpp>
  26. #include <string>
  27. #include <iostream>
  28. #include <cassert>
  29. boost::future<void> p(boost::future<void> f) {
  30. assert(f.is_ready());
  31. return boost::make_ready_future();
  32. }
  33. void p1()
  34. {
  35. // std::cout << BOOST_CONTEXTOF << std::endl;
  36. //boost::this_thread::sleep_for(boost::chrono::milliseconds(200));
  37. }
  38. void p2()
  39. {
  40. // std::cout << BOOST_CONTEXTOF << std::endl;
  41. //boost::this_thread::sleep_for(boost::chrono::seconds(10));
  42. }
  43. int f1()
  44. {
  45. std::cout << BOOST_CONTEXTOF << std::endl;
  46. boost::this_thread::sleep_for(boost::chrono::seconds(1));
  47. std::cout << BOOST_CONTEXTOF << std::endl;
  48. return 1;
  49. }
  50. int f2(int i)
  51. {
  52. // std::cout << BOOST_CONTEXTOF << std::endl;
  53. boost::this_thread::sleep_for(boost::chrono::seconds(2));
  54. return i + 1;
  55. }
  56. void submit_some(boost::executor& tp)
  57. {
  58. for (int i = 0; i < 3; ++i) {
  59. tp.submit(&p2);
  60. }
  61. for (int i = 0; i < 3; ++i) {
  62. tp.submit(&p1);
  63. }
  64. }
  65. void at_th_entry(boost::basic_thread_pool& )
  66. {
  67. }
  68. int test_executor_adaptor()
  69. {
  70. std::cout << BOOST_CONTEXTOF << std::endl;
  71. {
  72. try
  73. {
  74. {
  75. boost::executor_adaptor < boost::basic_thread_pool > ea(4);
  76. std::cout << BOOST_CONTEXTOF << std::endl;
  77. submit_some( ea);
  78. std::cout << BOOST_CONTEXTOF << std::endl;
  79. #if 1
  80. // fixme
  81. // ERROR= tr1::bad_weak_ptr
  82. {
  83. boost::future<int> t1 = boost::async(ea, &f1);
  84. boost::future<int> t2 = boost::async(ea, &f1);
  85. std::cout << BOOST_CONTEXTOF << " t1= " << t1.get() << std::endl;
  86. std::cout << BOOST_CONTEXTOF << " t2= " << t2.get() << std::endl;
  87. }
  88. std::cout << BOOST_CONTEXTOF << std::endl;
  89. submit_some(ea);
  90. std::cout << BOOST_CONTEXTOF << std::endl;
  91. {
  92. boost::basic_thread_pool ea3(1);
  93. std::cout << BOOST_CONTEXTOF << std::endl;
  94. boost::future<int> t1 = boost::async(ea3, &f1);
  95. std::cout << BOOST_CONTEXTOF << std::endl;
  96. boost::future<int> t2 = boost::async(ea3, &f1);
  97. std::cout << BOOST_CONTEXTOF << std::endl;
  98. //boost::future<int> t2 = boost::async(ea3, f2, 1); // todo this doesn't compiles yet on C++11
  99. //boost::future<int> t2 = boost::async(ea3, boost::bind(f2, 1)); // todo this doesn't compiles yet on C++98
  100. std::cout << BOOST_CONTEXTOF << " t1= " << t1.get() << std::endl;
  101. std::cout << BOOST_CONTEXTOF << " t2= " << t2.get() << std::endl;
  102. }
  103. #endif
  104. std::cout << BOOST_CONTEXTOF << std::endl;
  105. submit_some(ea);
  106. std::cout << BOOST_CONTEXTOF << std::endl;
  107. }
  108. std::cout << BOOST_CONTEXTOF << std::endl;
  109. {
  110. boost::executor_adaptor < boost::loop_executor > ea2;
  111. submit_some( ea2);
  112. ea2.underlying_executor().run_queued_closures();
  113. }
  114. #if ! defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  115. std::cout << BOOST_CONTEXTOF << std::endl;
  116. {
  117. boost::executor_adaptor < boost::basic_thread_pool > ea1(4);
  118. boost::executor_adaptor < boost::serial_executor > ea2(ea1);
  119. submit_some(ea2);
  120. }
  121. #endif
  122. std::cout << BOOST_CONTEXTOF << std::endl;
  123. {
  124. boost::executor_adaptor < boost::inline_executor > ea1;
  125. submit_some(ea1);
  126. }
  127. std::cout << BOOST_CONTEXTOF << std::endl;
  128. {
  129. boost::executor_adaptor < boost::thread_executor > ea1;
  130. submit_some(ea1);
  131. }
  132. std::cout << BOOST_CONTEXTOF << std::endl;
  133. #if 1
  134. // fixme
  135. // ERROR= tr1::bad_weak_ptr
  136. {
  137. boost::basic_thread_pool ea(4, at_th_entry);
  138. boost::future<int> t1 = boost::async(ea, &f1);
  139. std::cout << BOOST_CONTEXTOF << " t1= " << t1.get() << std::endl;
  140. }
  141. #endif
  142. std::cout << BOOST_CONTEXTOF << std::endl;
  143. {
  144. boost::async(&f1);
  145. }
  146. #if 1
  147. // fixme
  148. // ERROR= tr1::bad_weak_ptr
  149. std::cout << BOOST_CONTEXTOF << std::endl;
  150. {
  151. boost::basic_thread_pool ea(1);
  152. boost::async(ea,&f1);
  153. }
  154. #endif
  155. std::cout << BOOST_CONTEXTOF << std::endl;
  156. boost::this_thread::sleep_for(boost::chrono::milliseconds(200));
  157. std::cout << BOOST_CONTEXTOF << std::endl;
  158. }
  159. catch (std::exception& ex)
  160. {
  161. std::cout << "ERROR= " << ex.what() << "" << std::endl;
  162. return 1;
  163. }
  164. catch (...)
  165. {
  166. std::cout << " ERROR= exception thrown" << std::endl;
  167. return 2;
  168. }
  169. }
  170. // std::cout << BOOST_CONTEXTOF << std::endl;
  171. return 0;
  172. }
  173. int main()
  174. {
  175. return test_executor_adaptor();
  176. #if 0 && defined BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION \
  177. && defined BOOST_THREAD_PROVIDES_EXECUTORS \
  178. && ! defined BOOST_NO_CXX11_RVALUE_REFERENCES
  179. boost::basic_thread_pool executor;
  180. // compiles
  181. boost::make_ready_future().then(&p);
  182. // ??
  183. boost::make_ready_future().then(executor, &p);
  184. // doesn't compile
  185. boost::make_ready_future().then(executor, &p);
  186. #endif
  187. }