|
11 | 11 |
|
12 | 12 | namespace network { namespace utils {
|
13 | 13 |
|
14 |
| -struct thread_pool_pimpl { |
| 14 | + struct thread_pool_pimpl { |
15 | 15 | thread_pool_pimpl(
|
16 |
| - std::size_t threads = 1, |
17 |
| - io_service_ptr io_service = io_service_ptr(), |
18 |
| - worker_threads_ptr worker_threads = worker_threads_ptr() |
19 |
| - ) |
| 16 | + std::size_t threads = 1, |
| 17 | + io_service_ptr io_service = io_service_ptr(), |
| 18 | + worker_threads_ptr worker_threads = worker_threads_ptr() |
| 19 | + ) |
20 | 20 | : threads_(threads)
|
21 | 21 | , io_service_(io_service)
|
22 | 22 | , worker_threads_(worker_threads)
|
23 | 23 | , sentinel_()
|
24 | 24 | {
|
25 |
| - bool commit = false; |
26 |
| - BOOST_SCOPE_EXIT((&commit)(&io_service_)(&worker_threads_)(&sentinel_)) { |
27 |
| - if (!commit) { |
28 |
| - sentinel_.reset(); |
29 |
| - io_service_.reset(); |
30 |
| - if (worker_threads_.get()) { |
31 |
| - worker_threads_->interrupt_all(); |
32 |
| - worker_threads_->join_all(); |
33 |
| - } |
34 |
| - worker_threads_.reset(); |
35 |
| - } |
36 |
| - } BOOST_SCOPE_EXIT_END |
37 |
| - |
38 |
| - if (!io_service_.get()) { |
39 |
| - io_service_.reset(new boost::asio::io_service); |
| 25 | + bool commit = false; |
| 26 | + BOOST_SCOPE_EXIT((&commit)(&io_service_)(&worker_threads_)(&sentinel_)) { |
| 27 | + if (!commit) { |
| 28 | + sentinel_.reset(); |
| 29 | + io_service_.reset(); |
| 30 | + if (worker_threads_.get()) { |
| 31 | + worker_threads_->interrupt_all(); |
| 32 | + worker_threads_->join_all(); |
| 33 | + } |
| 34 | + worker_threads_.reset(); |
40 | 35 | }
|
| 36 | + } BOOST_SCOPE_EXIT_END |
41 | 37 |
|
42 |
| - if (!worker_threads_.get()) { |
43 |
| - worker_threads_.reset(new boost::thread_group); |
44 |
| - } |
| 38 | + if (!io_service_.get()) { |
| 39 | + io_service_.reset(new boost::asio::io_service); |
| 40 | + } |
45 | 41 |
|
46 |
| - if (!sentinel_.get()) { |
47 |
| - sentinel_.reset(new boost::asio::io_service::work(*io_service_)); |
48 |
| - } |
| 42 | + if (!worker_threads_.get()) { |
| 43 | + worker_threads_.reset(new boost::thread_group); |
| 44 | + } |
49 | 45 |
|
50 |
| - for (std::size_t counter = 0; counter < threads_; ++counter) |
51 |
| - worker_threads_->create_thread( |
52 |
| - boost::bind( |
53 |
| - &boost::asio::io_service::run, |
54 |
| - io_service_ |
55 |
| - ) |
56 |
| - ); |
| 46 | + if (!sentinel_.get()) { |
| 47 | + sentinel_.reset(new boost::asio::io_service::work(*io_service_)); |
| 48 | + } |
57 | 49 |
|
58 |
| - commit = true; |
| 50 | + for (std::size_t counter = 0; counter < threads_; ++counter) |
| 51 | + worker_threads_->create_thread( |
| 52 | + boost::bind( |
| 53 | + &boost::asio::io_service::run, |
| 54 | + io_service_ |
| 55 | + ) |
| 56 | + ); |
| 57 | + |
| 58 | + commit = true; |
59 | 59 | }
|
60 | 60 |
|
61 | 61 | std::size_t const thread_count() const {
|
62 |
| - return threads_; |
| 62 | + return threads_; |
63 | 63 | }
|
64 | 64 |
|
65 | 65 | void post(boost::function<void()> f) {
|
66 |
| - io_service_->post(f); |
| 66 | + io_service_->post(f); |
67 | 67 | }
|
68 | 68 |
|
69 | 69 | ~thread_pool_pimpl() {
|
70 |
| - sentinel_.reset(); |
71 |
| - try { |
72 |
| - worker_threads_->join_all(); |
73 |
| - } catch (...) { |
74 |
| - BOOST_ASSERT(false && "A handler was not supposed to throw, but one did."); |
75 |
| - std::abort(); |
76 |
| - } |
| 70 | + sentinel_.reset(); |
| 71 | + try { |
| 72 | + worker_threads_->join_all(); |
| 73 | + } catch (...) { |
| 74 | + BOOST_ASSERT(false && "A handler was not supposed to throw, but one did."); |
| 75 | + std::abort(); |
| 76 | + } |
77 | 77 | }
|
78 | 78 |
|
79 | 79 | void swap(thread_pool_pimpl & other) {
|
80 |
| - std::swap(other.threads_, threads_); |
81 |
| - std::swap(other.io_service_, io_service_); |
82 |
| - std::swap(other.worker_threads_, worker_threads_); |
83 |
| - std::swap(other.sentinel_, sentinel_); |
| 80 | + std::swap(other.threads_, threads_); |
| 81 | + std::swap(other.io_service_, io_service_); |
| 82 | + std::swap(other.worker_threads_, worker_threads_); |
| 83 | + std::swap(other.sentinel_, sentinel_); |
84 | 84 | }
|
85 |
| -protected: |
| 85 | + protected: |
86 | 86 | std::size_t threads_;
|
87 | 87 | io_service_ptr io_service_;
|
88 | 88 | worker_threads_ptr worker_threads_;
|
89 | 89 | sentinel_ptr sentinel_;
|
90 | 90 |
|
91 |
| -private: |
| 91 | + private: |
92 | 92 | thread_pool_pimpl(thread_pool_pimpl const &); // no copies please
|
93 | 93 | thread_pool_pimpl & operator=(thread_pool_pimpl); // no assignment please
|
94 |
| -}; |
| 94 | + }; |
95 | 95 |
|
96 |
| -thread_pool::thread_pool(std::size_t threads, |
97 |
| - io_service_ptr io_service, |
98 |
| - worker_threads_ptr worker_threads) |
99 |
| -: pimpl(new (std::nothrow) thread_pool_pimpl(threads, io_service, worker_threads)) |
100 |
| -{} |
| 96 | + thread_pool::thread_pool(std::size_t threads, |
| 97 | + io_service_ptr io_service, |
| 98 | + worker_threads_ptr worker_threads) |
| 99 | + : pimpl(new (std::nothrow) thread_pool_pimpl(threads, io_service, worker_threads)) |
| 100 | + {} |
101 | 101 |
|
102 |
| -std::size_t const thread_pool::thread_count() const { |
103 |
| - return pimpl->thread_count(); |
104 |
| -} |
| 102 | + std::size_t const thread_pool::thread_count() const { |
| 103 | + return pimpl->thread_count(); |
| 104 | + } |
105 | 105 |
|
106 |
| -void thread_pool::post(boost::function<void()> f) { |
107 |
| - pimpl->post(f); |
108 |
| -} |
109 |
| - |
110 |
| -void thread_pool::swap(thread_pool & other) { |
111 |
| - std::swap(other.pimpl, this->pimpl); |
112 |
| -} |
113 |
| - |
114 |
| -thread_pool::~thread_pool() { |
115 |
| - delete pimpl; |
116 |
| -} |
| 106 | + void thread_pool::post(boost::function<void()> f) { |
| 107 | + pimpl->post(f); |
| 108 | + } |
117 | 109 |
|
| 110 | + void thread_pool::swap(thread_pool & other) { |
| 111 | + std::swap(other.pimpl, this->pimpl); |
| 112 | + } |
| 113 | + |
| 114 | + thread_pool::~thread_pool() { |
| 115 | + delete pimpl; |
| 116 | + } |
| 117 | + |
118 | 118 | } // namespace utils
|
119 | 119 | } // namespace network
|
120 |
| - |
| 120 | + |
121 | 121 | #endif /* NETWORK_UTILS_THREAD_POOL_IPP_20111021 */
|
0 commit comments