serial_executor.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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.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& tp)
  49. {
  50. for (int i = 0; i < 3; ++i) {
  51. std::cout << BOOST_CONTEXTOF << std::endl;
  52. tp.submit(&p2);
  53. }
  54. for (int i = 0; i < 3; ++i) {
  55. std::cout << BOOST_CONTEXTOF << std::endl;
  56. tp.submit(&p1);
  57. }
  58. }
  59. void at_th_entry(boost::basic_thread_pool& )
  60. {
  61. }
  62. int test_executor_adaptor()
  63. {
  64. {
  65. try
  66. {
  67. #if ! defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  68. {
  69. boost::basic_thread_pool ea1(4);
  70. boost::serial_executor ea2(ea1);
  71. submit_some(ea2);
  72. boost::this_thread::sleep_for(boost::chrono::seconds(10));
  73. }
  74. #endif
  75. }
  76. catch (std::exception& ex)
  77. {
  78. std::cout << "ERROR= " << ex.what() << "" << std::endl;
  79. return 1;
  80. }
  81. catch (...)
  82. {
  83. std::cout << " ERROR= exception thrown" << std::endl;
  84. return 2;
  85. }
  86. }
  87. return 0;
  88. }
  89. int main()
  90. {
  91. return test_executor_adaptor();
  92. }