thread_pool.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. #define BOOST_THREAD_VERSION 5
  7. //#define BOOST_THREAD_USES_LOG
  8. #define BOOST_THREAD_USES_LOG_THREAD_ID
  9. #define BOOST_THREAD_QUEUE_DEPRECATE_OLD
  10. #if ! defined BOOST_NO_CXX11_DECLTYPE
  11. #define BOOST_RESULT_OF_USE_DECLTYPE
  12. #endif
  13. #include <boost/thread/detail/log.hpp>
  14. #include <boost/thread/executors/basic_thread_pool.hpp>
  15. #include <boost/thread/thread_only.hpp>
  16. #include <boost/assert.hpp>
  17. #include <string>
  18. #ifdef BOOST_MSVC
  19. #pragma warning(disable: 4127) // conditional expression is constant
  20. #endif
  21. void p1()
  22. {
  23. BOOST_THREAD_LOG
  24. << boost::this_thread::get_id() << " P1" << BOOST_THREAD_END_LOG;
  25. }
  26. void p2()
  27. {
  28. BOOST_THREAD_LOG
  29. << boost::this_thread::get_id() << " P2" << BOOST_THREAD_END_LOG;
  30. }
  31. void submit_some(boost::basic_thread_pool& tp) {
  32. tp.submit(&p1);
  33. tp.submit(&p2);
  34. tp.submit(&p1);
  35. tp.submit(&p2);
  36. tp.submit(&p1);
  37. tp.submit(&p2);
  38. tp.submit(&p1);
  39. tp.submit(&p2);
  40. tp.submit(&p1);
  41. tp.submit(&p2);
  42. }
  43. int main()
  44. {
  45. BOOST_THREAD_LOG
  46. << boost::this_thread::get_id() << " <MAIN" << BOOST_THREAD_END_LOG;
  47. {
  48. try
  49. {
  50. boost::basic_thread_pool tp;
  51. submit_some(tp);
  52. }
  53. catch (std::exception& ex)
  54. {
  55. BOOST_THREAD_LOG
  56. << "ERRORRRRR " << ex.what() << "" << BOOST_THREAD_END_LOG;
  57. return 1;
  58. }
  59. catch (...)
  60. {
  61. BOOST_THREAD_LOG
  62. << " ERRORRRRR exception thrown" << BOOST_THREAD_END_LOG;
  63. return 2;
  64. }
  65. }
  66. BOOST_THREAD_LOG
  67. << boost::this_thread::get_id() << "MAIN>" << BOOST_THREAD_END_LOG;
  68. return 0;
  69. }