Go to the documentation of this file.00001
00017 #ifndef THREADPOOL_DETAIL_WORKER_THREAD_HPP_INCLUDED
00018 #define THREADPOOL_DETAIL_WORKER_THREAD_HPP_INCLUDED
00019
00020
00021 #include "scope_guard.hpp"
00022
00023 #include <boost/smart_ptr.hpp>
00024 #include <boost/thread.hpp>
00025 #include <boost/thread/exceptions.hpp>
00026 #include <boost/thread/mutex.hpp>
00027 #include <boost/bind.hpp>
00028
00029
00030 namespace boost { namespace threadpool { namespace detail
00031 {
00032
00043 template <typename Pool>
00044 class worker_thread
00045 : public enable_shared_from_this< worker_thread<Pool> >
00046 , private noncopyable
00047 {
00048 public:
00049 typedef Pool pool_type;
00050
00051 private:
00052 shared_ptr<pool_type> m_pool;
00053 shared_ptr<boost::thread> m_thread;
00054
00055
00060 worker_thread(shared_ptr<pool_type> const & pool)
00061 : m_pool(pool)
00062 {
00063 assert(pool);
00064 }
00065
00066
00069 void died_unexpectedly()
00070 {
00071 m_pool->worker_died_unexpectedly(this->shared_from_this());
00072 }
00073
00074
00075 public:
00078 void run()
00079 {
00080 scope_guard notify_exception(bind(&worker_thread::died_unexpectedly, this));
00081
00082 while(m_pool->execute_task()) {}
00083
00084 notify_exception.disable();
00085 m_pool->worker_destructed(this->shared_from_this());
00086 }
00087
00088
00091 void join()
00092 {
00093 m_thread->join();
00094 }
00095
00096
00100 static void create_and_attach(shared_ptr<pool_type> const & pool)
00101 {
00102 shared_ptr<worker_thread> worker(new worker_thread(pool));
00103 if(worker)
00104 {
00105 worker->m_thread.reset(new boost::thread(bind(&worker_thread::run, worker)));
00106 }
00107 }
00108
00109 };
00110
00111
00112 } } }
00113
00114 #endif // THREADPOOL_DETAIL_WORKER_THREAD_HPP_INCLUDED
00115