serial_executor_cont.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright (C) 2015 Vicente J. Botet Escriba
  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/serial_executor_cont.hpp>
  17. #include <boost/thread/executors/executor.hpp>
  18. #include <boost/thread/executors/executor_adaptor.hpp>
  19. #include <boost/thread/executor.hpp>
  20. #include <boost/thread/future.hpp>
  21. #include <boost/assert.hpp>
  22. #include <string>
  23. #include <iostream>
  24. void p1()
  25. {
  26. std::cout << BOOST_CONTEXTOF << std::endl;
  27. boost::this_thread::sleep_for(boost::chrono::milliseconds(30));
  28. std::cout << BOOST_CONTEXTOF << std::endl;
  29. }
  30. void p2()
  31. {
  32. std::cout << BOOST_CONTEXTOF << std::endl;
  33. boost::this_thread::sleep_for(boost::chrono::milliseconds(10));
  34. std::cout << BOOST_CONTEXTOF << std::endl;
  35. }
  36. int f1()
  37. {
  38. // std::cout << BOOST_CONTEXTOF << std::endl;
  39. boost::this_thread::sleep_for(boost::chrono::seconds(1));
  40. return 1;
  41. }
  42. int f2(int i)
  43. {
  44. // std::cout << BOOST_CONTEXTOF << std::endl;
  45. boost::this_thread::sleep_for(boost::chrono::seconds(2));
  46. return i + 1;
  47. }
  48. void submit_some(boost::serial_executor_cont& tp)
  49. {
  50. std::cout << BOOST_CONTEXTOF << std::endl;
  51. for (int i = 0; i < 3; ++i) {
  52. std::cout << BOOST_CONTEXTOF << std::endl;
  53. tp.submit(&p2);
  54. }
  55. for (int i = 0; i < 3; ++i) {
  56. std::cout << BOOST_CONTEXTOF << std::endl;
  57. tp.submit(&p1);
  58. }
  59. std::cout << BOOST_CONTEXTOF << std::endl;
  60. }
  61. void at_th_entry(boost::basic_thread_pool& )
  62. {
  63. }
  64. int test_executor_adaptor()
  65. {
  66. // std::cout << BOOST_CONTEXTOF << std::endl;
  67. {
  68. try
  69. {
  70. #if ! defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  71. // std::cout << BOOST_CONTEXTOF << std::endl;
  72. {
  73. boost::basic_thread_pool ea1(4);
  74. boost::serial_executor_cont ea2(ea1);
  75. submit_some(ea2);
  76. boost::this_thread::sleep_for(boost::chrono::seconds(10));
  77. }
  78. #endif
  79. // std::cout << BOOST_CONTEXTOF << std::endl;
  80. }
  81. catch (std::exception& ex)
  82. {
  83. std::cout << "ERROR= " << ex.what() << "" << std::endl;
  84. return 1;
  85. }
  86. catch (...)
  87. {
  88. std::cout << " ERROR= exception thrown" << std::endl;
  89. return 2;
  90. }
  91. }
  92. // std::cout << BOOST_CONTEXTOF << std::endl;
  93. return 0;
  94. }
  95. int main()
  96. {
  97. return test_executor_adaptor();
  98. }