/BJEngine/src/ThreadPool/threadpool/detail/worker_thread.hpp

https://bitbucket.org/mild1/bj · C++ Header · 115 lines · 52 code · 25 blank · 38 comment · 2 complexity · af4ca8e2f67e7a14fabca6404a87dbc8 MD5 · raw file

  1. /*! \file
  2. * \brief Thread pool worker.
  3. *
  4. * The worker thread instance is attached to a pool
  5. * and executes tasks of this pool.
  6. *
  7. * Copyright (c) 2005-2007 Philipp Henkel
  8. *
  9. * Use, modification, and distribution are subject to the
  10. * Boost Software License, Version 1.0. (See accompanying file
  11. * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  12. *
  13. * http://threadpool.sourceforge.net
  14. *
  15. */
  16. #ifndef THREADPOOL_DETAIL_WORKER_THREAD_HPP_INCLUDED
  17. #define THREADPOOL_DETAIL_WORKER_THREAD_HPP_INCLUDED
  18. #include "scope_guard.hpp"
  19. #include <boost/smart_ptr.hpp>
  20. #include <boost/thread.hpp>
  21. #include <boost/thread/exceptions.hpp>
  22. #include <boost/thread/mutex.hpp>
  23. #include <boost/bind.hpp>
  24. namespace boost { namespace threadpool { namespace detail
  25. {
  26. /*! \brief Thread pool worker.
  27. *
  28. * A worker_thread represents a thread of execution. The worker is attached to a
  29. * thread pool and processes tasks of that pool. The lifetime of the worker and its
  30. * internal boost::thread is managed automatically.
  31. *
  32. * This class is a helper class and cannot be constructed or accessed directly.
  33. *
  34. * \see pool_core
  35. */
  36. template <typename Pool>
  37. class worker_thread
  38. : public enable_shared_from_this< worker_thread<Pool> >
  39. , private noncopyable
  40. {
  41. public:
  42. typedef Pool pool_type; //!< Indicates the pool's type.
  43. private:
  44. shared_ptr<pool_type> m_pool; //!< Pointer to the pool which created the worker.
  45. shared_ptr<boost::thread> m_thread; //!< Pointer to the thread which executes the run loop.
  46. /*! Constructs a new worker.
  47. * \param pool Pointer to it's parent pool.
  48. * \see function create_and_attach
  49. */
  50. worker_thread(shared_ptr<pool_type> const & pool)
  51. : m_pool(pool)
  52. {
  53. assert(pool);
  54. }
  55. /*! Notifies that an exception occurred in the run loop.
  56. */
  57. void died_unexpectedly()
  58. {
  59. m_pool->worker_died_unexpectedly(this->shared_from_this());
  60. }
  61. public:
  62. /*! Executes pool's tasks sequentially.
  63. */
  64. void run()
  65. {
  66. scope_guard notify_exception(bind(&worker_thread::died_unexpectedly, this));
  67. while(m_pool->execute_task()) {}
  68. notify_exception.disable();
  69. m_pool->worker_destructed(this->shared_from_this());
  70. }
  71. /*! Joins the worker's thread.
  72. */
  73. void join()
  74. {
  75. m_thread->join();
  76. }
  77. /*! Constructs a new worker thread and attaches it to the pool.
  78. * \param pool Pointer to the pool.
  79. */
  80. static void create_and_attach(shared_ptr<pool_type> const & pool)
  81. {
  82. shared_ptr<worker_thread> worker(new worker_thread(pool));
  83. if(worker)
  84. {
  85. worker->m_thread.reset(new boost::thread(bind(&worker_thread::run, worker)));
  86. }
  87. }
  88. };
  89. } } } // namespace boost::threadpool::detail
  90. #endif // THREADPOOL_DETAIL_WORKER_THREAD_HPP_INCLUDED