berkeyvx
berkeyvx

Reputation: 51

why boost asio tcp resolver creates threads for async resolve

When I use the following code, it spawns a new thread.

resolver_.async_resolve(
      host_, port_,
      beast::bind_front_handler(&session::on_resolve, shared_from_this()));

But when I change it with non-async version, it does not create a new thread.

  boost::system::error_code err;
  auto const& results = resolver_.resolve(host_, port_, err);
  on_resolve(err, results);

Resolver definition:

boost::asio::ip::tcp::resolver resolver_;

This behaviour is not documented anywhere and I have been trying to understand for a hour where this thread is spawned because it basically empty thread and does not destructed anytime. Here is the screenshot of the thread details.

enter image description here

Boost version: boost/1.82.0 What is the purpose of this thread, and how is it related to the boost tcp async resolve?

Upvotes: 2

Views: 84

Answers (1)

sehe
sehe

Reputation: 393769

The creation of the thread is very exactly governed by the corresponding service implementation in the execution context. This is an implementation detail, that you should not worry about.

It is documented here under "Platform Specific Implementation Notes":

An additional thread per io_context is used to emulate asynchronous host resolution. This thread is created on the first call to either ip::tcp::resolver::async_resolve() or ip::udp::resolver::async_resolve().

Upvotes: 2

Related Questions