Whats the difference between an asio::thread_pool
and an asio::io_context
whose run()
function is called from multiple threads? Can I replace my boost::thread_group
of threads that call io_context::run()
with an asio::thread_pool
? Or do I need somewhere an io_context
?
Update
When I use asio::thread_pool
, do I still need an io_context
to use sockets, timers, etc? Both thread_pool
and io_context
are an asio::execution_context
. However, the docs say on io_context
that it "Provides core I/O functionality". Do I lose these if I only use an asio::thread_pool
without an io_context
?
Asio socket, a stream is not thread safe. Callers are responsible for synchronizing operations on the socket using an implicit or explicit strand, as per the Asio documentation.
The io_context class provides the core I/O functionality for users of the asynchronous I/O objects, including: boost::asio::ip::tcp::socket.
It is safe to post handlers from within a handler for a single instance of an io_service according to the documentation.
Asio defines boost::asio::io_service , a single class for an I/O service object. Every program based on Boost. Asio uses an object of type boost::asio::io_service . This can also be a global variable. While there is only one class for an I/O service object, several classes for I/O objects exist.
A threadpool implicit runs all the tasks posted on it (until it's stopped).
An io_service doesn't assume anything about the threads that will run it: you need to make sure you do that, and you're free to decide whether you run it on multiple threads, one thread, or even a mix (like one thread at at time, but from multiple threads?).
Further notes:
io_service
run/poll members (Should the exception thrown by boost::asio::io_service::run() be caught?)io_service
can be restarted (after a reset()
). asio::thread_pool
not so much (see [search is dead atm], compare with asio::io_service and thread_group lifecycle issue)asio::thread_pool
are "opaque": you cannot control how they're created. Which is a bummer if you need to e.g. initialize a third party library per thread or wanted to use Boost Thread with interruption_point
s etc.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With