]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/asio/example/cpp03/http/server2/io_service_pool.cpp
5 // Copyright (c) 2003-2016 Christopher M. Kohlhoff (chris at kohlhoff dot com)
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
13 #include <boost/thread/thread.hpp>
14 #include <boost/bind.hpp>
15 #include <boost/shared_ptr.hpp>
20 io_service_pool::io_service_pool(std::size_t pool_size
)
24 throw std::runtime_error("io_service_pool size is 0");
26 // Give all the io_services work to do so that their run() functions will not
27 // exit until they are explicitly stopped.
28 for (std::size_t i
= 0; i
< pool_size
; ++i
)
30 io_service_ptr
io_service(new boost::asio::io_service
);
31 work_ptr
work(new boost::asio::io_service::work(*io_service
));
32 io_services_
.push_back(io_service
);
33 work_
.push_back(work
);
37 void io_service_pool::run()
39 // Create a pool of threads to run all of the io_services.
40 std::vector
<boost::shared_ptr
<boost::thread
> > threads
;
41 for (std::size_t i
= 0; i
< io_services_
.size(); ++i
)
43 boost::shared_ptr
<boost::thread
> thread(new boost::thread(
44 boost::bind(&boost::asio::io_service::run
, io_services_
[i
])));
45 threads
.push_back(thread
);
48 // Wait for all threads in the pool to exit.
49 for (std::size_t i
= 0; i
< threads
.size(); ++i
)
53 void io_service_pool::stop()
55 // Explicitly stop all io_services.
56 for (std::size_t i
= 0; i
< io_services_
.size(); ++i
)
57 io_services_
[i
]->stop();
60 boost::asio::io_service
& io_service_pool::get_io_service()
62 // Use a round-robin scheme to choose the next io_service to use.
63 boost::asio::io_service
& io_service
= *io_services_
[next_io_service_
];
65 if (next_io_service_
== io_services_
.size())
70 } // namespace server2