/BJEngine/src/ThreadPool/threadpool/detail/worker_thread.hpp
C++ Header | 115 lines | 52 code | 25 blank | 38 comment | 1 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
17#ifndef THREADPOOL_DETAIL_WORKER_THREAD_HPP_INCLUDED
18#define THREADPOOL_DETAIL_WORKER_THREAD_HPP_INCLUDED
19
20
21#include "scope_guard.hpp"
22
23#include <boost/smart_ptr.hpp>
24#include <boost/thread.hpp>
25#include <boost/thread/exceptions.hpp>
26#include <boost/thread/mutex.hpp>
27#include <boost/bind.hpp>
28
29
30namespace boost { namespace threadpool { namespace detail
31{
32
33 /*! \brief Thread pool worker.
34 *
35 * A worker_thread represents a thread of execution. The worker is attached to a
36 * thread pool and processes tasks of that pool. The lifetime of the worker and its
37 * internal boost::thread is managed automatically.
38 *
39 * This class is a helper class and cannot be constructed or accessed directly.
40 *
41 * \see pool_core
42 */
43 template <typename Pool>
44 class worker_thread
45 : public enable_shared_from_this< worker_thread<Pool> >
46 , private noncopyable
47 {
48 public:
49 typedef Pool pool_type; //!< Indicates the pool's type.
50
51 private:
52 shared_ptr<pool_type> m_pool; //!< Pointer to the pool which created the worker.
53 shared_ptr<boost::thread> m_thread; //!< Pointer to the thread which executes the run loop.
54
55
56 /*! Constructs a new worker.
57 * \param pool Pointer to it's parent pool.
58 * \see function create_and_attach
59 */
60 worker_thread(shared_ptr<pool_type> const & pool)
61 : m_pool(pool)
62 {
63 assert(pool);
64 }
65
66
67 /*! Notifies that an exception occurred in the run loop.
68 */
69 void died_unexpectedly()
70 {
71 m_pool->worker_died_unexpectedly(this->shared_from_this());
72 }
73
74
75 public:
76 /*! Executes pool's tasks sequentially.
77 */
78 void run()
79 {
80 scope_guard notify_exception(bind(&worker_thread::died_unexpectedly, this));
81
82 while(m_pool->execute_task()) {}
83
84 notify_exception.disable();
85 m_pool->worker_destructed(this->shared_from_this());
86 }
87
88
89 /*! Joins the worker's thread.
90 */
91 void join()
92 {
93 m_thread->join();
94 }
95
96
97 /*! Constructs a new worker thread and attaches it to the pool.
98 * \param pool Pointer to the pool.
99 */
100 static void create_and_attach(shared_ptr<pool_type> const & pool)
101 {
102 shared_ptr<worker_thread> worker(new worker_thread(pool));
103 if(worker)
104 {
105 worker->m_thread.reset(new boost::thread(bind(&worker_thread::run, worker)));
106 }
107 }
108
109 };
110
111
112} } } // namespace boost::threadpool::detail
113
114#endif // THREADPOOL_DETAIL_WORKER_THREAD_HPP_INCLUDED
115