|
8 | 8 |
|
9 | 9 | #include<cstddef> |
10 | 10 | #include<boost/network/tags.hpp> |
| 11 | +#include<boost/thread/thread.hpp> |
| 12 | +#include<boost/shared_ptr.hpp> |
| 13 | +#include<boost/function.hpp> |
| 14 | +#include<boost/asio/io_service.hpp> |
| 15 | +#include<boost/scope_exit.hpp> |
11 | 16 |
|
12 | 17 | namespaceboost {namespacenetwork {namespaceutils { |
| 18 | + |
| 19 | +typedef boost::shared_ptr<boost::asio::io_service> io_service_ptr; |
| 20 | +typedef boost::shared_ptr<boost::thread_group> worker_threads_ptr; |
| 21 | +typedef boost::shared_ptr<boost::asio::io_service::work> sentinel_ptr; |
13 | 22 |
|
14 | 23 | template<classTag> |
15 | 24 | structbasic_thread_pool { |
16 | | -basic_thread_pool(std::size_t threads =1) : threads_(threads) {} |
| 25 | +basic_thread_pool( |
| 26 | + std::size_t threads =1, |
| 27 | + io_service_ptr io_service = io_service_ptr(), |
| 28 | + worker_threads_ptr worker_threads = worker_threads_ptr() |
| 29 | + ) |
| 30 | + : threads_(threads) |
| 31 | + , io_service_(io_service) |
| 32 | + , worker_threads_(worker_threads) |
| 33 | + , sentinel_() |
| 34 | + { |
| 35 | +bool commit =false; |
| 36 | +BOOST_SCOPE_EXIT_TPL((&commit)(&io_service_)(&worker_threads_)(&sentinel_)) { |
| 37 | +if (!commit) { |
| 38 | + sentinel_.reset(); |
| 39 | + io_service_.reset(); |
| 40 | +if (worker_threads_.get()) { |
| 41 | + worker_threads_->interrupt_all(); |
| 42 | + worker_threads_->join_all(); |
| 43 | + } |
| 44 | + worker_threads_.reset(); |
| 45 | + } |
| 46 | + } BOOST_SCOPE_EXIT_END |
| 47 | + |
| 48 | +if (!io_service_.get()) { |
| 49 | + io_service_.reset(new boost::asio::io_service); |
| 50 | + } |
| 51 | + |
| 52 | +if (!worker_threads_.get()) { |
| 53 | + worker_threads_.reset(new boost::thread_group); |
| 54 | + } |
| 55 | + |
| 56 | +if (!sentinel_.get()) { |
| 57 | + sentinel_.reset(newboost::asio::io_service::work(*io_service_)); |
| 58 | + } |
| 59 | + |
| 60 | +for (std::size_t counter =0; counter < threads_; ++counter) |
| 61 | + worker_threads_->create_thread( |
| 62 | +boost::bind( |
| 63 | + &boost::asio::io_service::run, |
| 64 | + io_service_ |
| 65 | + ) |
| 66 | + ); |
| 67 | + |
| 68 | + commit =true; |
| 69 | + } |
| 70 | + |
17 | 71 | std::size_tconstthread_count()const { |
18 | 72 | return threads_; |
19 | 73 | } |
| 74 | + |
| 75 | +voidpost(boost::function<void()> f) { |
| 76 | + io_service_->post(f); |
| 77 | + } |
| 78 | + |
| 79 | +~basic_thread_pool()throw () { |
| 80 | + sentinel_.reset(); |
| 81 | +try { |
| 82 | + worker_threads_->join_all(); |
| 83 | + }catch (...) { |
| 84 | +BOOST_ASSERT(false &&"A handler was not supposed to throw, but one did."); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | +voidswap(basic_thread_pool & other) { |
| 89 | +std::swap(other.threads_, threads_); |
| 90 | +std::swap(other.io_service_, io_service_); |
| 91 | +std::swap(other.worket_threads_, worker_threads_); |
| 92 | +std::swap(other.sentinel_, sentinel_); |
| 93 | + } |
20 | 94 | protected: |
21 | 95 | std::size_t threads_; |
| 96 | + io_service_ptr io_service_; |
| 97 | + worker_threads_ptr worker_threads_; |
| 98 | + sentinel_ptr sentinel_; |
| 99 | + |
| 100 | +private: |
| 101 | +basic_thread_pool(basic_thread_poolconst &);// no copies please |
| 102 | + basic_thread_pool &operator=(basic_thread_pool);// no assignment please |
22 | 103 | }; |
23 | 104 |
|
24 | 105 | typedef basic_thread_pool<tags::default_> thread_pool; |
|