generic_executor_ref.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Copyright (C) 2014 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. #define BOOST_THREAD_VERSION 4
  10. #define BOOST_THREAD_PROVIDES_EXECUTORS
  11. //#define BOOST_THREAD_USES_LOG
  12. #define BOOST_THREAD_USES_LOG_THREAD_ID
  13. #define BOOST_THREAD_QUEUE_DEPRECATE_OLD
  14. #include <boost/thread/caller_context.hpp>
  15. #include <boost/thread/executors/basic_thread_pool.hpp>
  16. #include <boost/thread/executors/loop_executor.hpp>
  17. #include <boost/thread/executors/serial_executor.hpp>
  18. #include <boost/thread/executors/inline_executor.hpp>
  19. #include <boost/thread/executors/thread_executor.hpp>
  20. #include <boost/thread/executors/executor.hpp>
  21. #include <boost/thread/executors/executor_adaptor.hpp>
  22. #include <boost/thread/executor.hpp>
  23. #include <boost/thread/future.hpp>
  24. #include <boost/assert.hpp>
  25. #include <string>
  26. #include <iostream>
  27. void p1()
  28. {
  29. // std::cout << BOOST_CONTEXTOF << std::endl;
  30. //boost::this_thread::sleep_for(boost::chrono::milliseconds(200));
  31. }
  32. void p2()
  33. {
  34. // std::cout << BOOST_CONTEXTOF << std::endl;
  35. //boost::this_thread::sleep_for(boost::chrono::seconds(10));
  36. }
  37. int f1()
  38. {
  39. // std::cout << BOOST_CONTEXTOF << std::endl;
  40. boost::this_thread::sleep_for(boost::chrono::seconds(1));
  41. return 1;
  42. }
  43. int f2(int i)
  44. {
  45. // std::cout << BOOST_CONTEXTOF << std::endl;
  46. boost::this_thread::sleep_for(boost::chrono::seconds(2));
  47. return i + 1;
  48. }
  49. void submit_some(boost::generic_executor_ref tp)
  50. {
  51. for (int i = 0; i < 3; ++i) {
  52. tp.submit(&p2);
  53. }
  54. for (int i = 0; i < 3; ++i) {
  55. tp.submit(&p1);
  56. }
  57. }
  58. void at_th_entry(boost::basic_thread_pool& )
  59. {
  60. }
  61. int test_generic_executor_ref()
  62. {
  63. std::cout << BOOST_CONTEXTOF << std::endl;
  64. {
  65. try
  66. {
  67. {
  68. boost::basic_thread_pool ea(4);
  69. submit_some( ea);
  70. {
  71. boost::future<int> t1 = boost::async(ea, &f1);
  72. boost::future<int> t2 = boost::async(ea, &f1);
  73. std::cout << BOOST_CONTEXTOF << " t1= " << t1.get() << std::endl;
  74. std::cout << BOOST_CONTEXTOF << " t2= " << t2.get() << std::endl;
  75. }
  76. submit_some(ea);
  77. {
  78. boost::basic_thread_pool ea3(1);
  79. boost::future<int> t1 = boost::async(ea3, &f1);
  80. boost::future<int> t2 = boost::async(ea3, &f1);
  81. //boost::future<int> t2 = boost::async(ea3, f2, 1); // todo this doesn't compiles yet on C++11
  82. //boost::future<int> t2 = boost::async(ea3, boost::bind(f2, 1)); // todo this doesn't compiles yet on C++98
  83. std::cout << BOOST_CONTEXTOF << " t1= " << t1.get() << std::endl;
  84. std::cout << BOOST_CONTEXTOF << " t2= " << t2.get() << std::endl;
  85. }
  86. submit_some(ea);
  87. }
  88. std::cout << BOOST_CONTEXTOF << std::endl;
  89. {
  90. boost::loop_executor ea2;
  91. submit_some( ea2);
  92. ea2.run_queued_closures();
  93. }
  94. #if ! defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  95. std::cout << BOOST_CONTEXTOF << std::endl;
  96. {
  97. boost::basic_thread_pool ea1(4);
  98. boost::serial_executor ea2(ea1);
  99. submit_some(ea2);
  100. }
  101. #endif
  102. std::cout << BOOST_CONTEXTOF << std::endl;
  103. {
  104. boost::inline_executor ea1;
  105. submit_some(ea1);
  106. }
  107. std::cout << BOOST_CONTEXTOF << std::endl;
  108. {
  109. //boost::thread_executor ea1;
  110. //submit_some(ea1);
  111. }
  112. std::cout << BOOST_CONTEXTOF << std::endl;
  113. {
  114. boost::basic_thread_pool ea(4, at_th_entry);
  115. boost::future<int> t1 = boost::async(ea, &f1);
  116. std::cout << BOOST_CONTEXTOF << " t1= " << t1.get() << std::endl;
  117. }
  118. std::cout << BOOST_CONTEXTOF << std::endl;
  119. {
  120. boost::basic_thread_pool ea(4, at_th_entry);
  121. boost::async(ea, &f1);
  122. std::cout << BOOST_CONTEXTOF << std::endl;
  123. }
  124. std::cout << BOOST_CONTEXTOF << std::endl;
  125. boost::this_thread::sleep_for(boost::chrono::milliseconds(200));
  126. std::cout << BOOST_CONTEXTOF << std::endl;
  127. }
  128. catch (std::exception& ex)
  129. {
  130. std::cout << "ERROR= " << ex.what() << "" << std::endl;
  131. return 1;
  132. }
  133. catch (...)
  134. {
  135. std::cout << " ERROR= exception thrown" << std::endl;
  136. return 2;
  137. }
  138. }
  139. // std::cout << BOOST_CONTEXTOF << std::endl;
  140. return 0;
  141. }
  142. int main()
  143. {
  144. return test_generic_executor_ref();
  145. }