Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between asio::io_context and asio::thread_pool?

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?

like image 669
Valar Morghulis Avatar asked May 18 '20 07:05

Valar Morghulis


People also ask

Is asio thread safe?

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.

What is Io_context?

The io_context class provides the core I/O functionality for users of the asynchronous I/O objects, including: boost::asio::ip::tcp::socket.

Is boost :: asio :: io_service thread safe?

It is safe to post handlers from within a handler for a single instance of an io_service according to the documentation.

What is boost :: asio :: io_service?

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.


1 Answers

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:

  • handle exceptions emanating from io_service run/poll members (Should the exception thrown by boost::asio::io_service::run() be caught?)
  • if you run an io_service on multiple threads, make sure you understand thread-safety of the service objects you use (see Why do I need strand per connection when using boost::asio?)
  • If you know how many threads will run your service, consider supplying the concurrency hint on construction (https://www.boost.org/doc/libs/1_66_0/doc/html/boost_asio/overview/core/concurrency_hint.html)
  • 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)
  • The threads governed by 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_points etc.
like image 152
sehe Avatar answered Oct 17 '22 09:10

sehe