priority_executor_base.hpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright (C) 2014 Ian Forbed
  2. // Copyright (C) 2014 Vicente J. Botet Escriba
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. #ifndef BOOST_THREAD_EXECUTORS_DETAIL_PRIORITY_EXECUTOR_BASE_HPP
  8. #define BOOST_THREAD_EXECUTORS_DETAIL_PRIORITY_EXECUTOR_BASE_HPP
  9. #include <boost/atomic.hpp>
  10. #include <boost/function.hpp>
  11. #include <boost/thread/thread.hpp>
  12. #include <boost/thread/concurrent_queues/sync_timed_queue.hpp>
  13. #include <boost/thread/executors/work.hpp>
  14. namespace boost
  15. {
  16. namespace executors
  17. {
  18. namespace detail
  19. {
  20. template <class Queue>
  21. class priority_executor_base
  22. {
  23. public:
  24. //typedef boost::function<void()> work;
  25. typedef executors::work_pq work;
  26. protected:
  27. typedef Queue queue_type;
  28. queue_type _workq;
  29. priority_executor_base() {}
  30. public:
  31. ~priority_executor_base()
  32. {
  33. if(!closed())
  34. {
  35. this->close();
  36. }
  37. }
  38. void close()
  39. {
  40. _workq.close();
  41. }
  42. bool closed()
  43. {
  44. return _workq.closed();
  45. }
  46. void loop()
  47. {
  48. try
  49. {
  50. for(;;)
  51. {
  52. try {
  53. work task;
  54. queue_op_status st = _workq.wait_pull(task);
  55. if (st == queue_op_status::closed) return;
  56. task();
  57. }
  58. catch (boost::thread_interrupted&)
  59. {
  60. return;
  61. }
  62. }
  63. }
  64. catch (...)
  65. {
  66. std::terminate();
  67. return;
  68. }
  69. }
  70. }; //end class
  71. } //end detail namespace
  72. } //end executors namespace
  73. } //end boost namespace
  74. #endif