]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/asio/example/cpp03/http/server2/io_context_pool.cpp
5 // Copyright (c) 2003-2022 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/bind.hpp>
15 #include <boost/shared_ptr.hpp>
20 io_context_pool::io_context_pool(std::size_t pool_size
)
24 throw std::runtime_error("io_context_pool size is 0");
26 // Give all the io_contexts 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_context_ptr
io_context(new boost::asio::io_context
);
31 io_contexts_
.push_back(io_context
);
32 work_
.push_back(boost::asio::make_work_guard(*io_context
));
36 void io_context_pool::run()
38 // Create a pool of threads to run all of the io_contexts.
39 std::vector
<boost::shared_ptr
<boost::thread
> > threads
;
40 for (std::size_t i
= 0; i
< io_contexts_
.size(); ++i
)
42 boost::shared_ptr
<boost::thread
> thread(new boost::thread(
43 boost::bind(&boost::asio::io_context::run
, io_contexts_
[i
])));
44 threads
.push_back(thread
);
47 // Wait for all threads in the pool to exit.
48 for (std::size_t i
= 0; i
< threads
.size(); ++i
)
52 void io_context_pool::stop()
54 // Explicitly stop all io_contexts.
55 for (std::size_t i
= 0; i
< io_contexts_
.size(); ++i
)
56 io_contexts_
[i
]->stop();
59 boost::asio::io_context
& io_context_pool::get_io_context()
61 // Use a round-robin scheme to choose the next io_context to use.
62 boost::asio::io_context
& io_context
= *io_contexts_
[next_io_context_
];
64 if (next_io_context_
== io_contexts_
.size())
69 } // namespace server2