Does a coroutine call boost::asio::write block the current thread?

79 Views Asked by At

When the coroutine calls boost::asio::write, the coroutine will block waiting for the buffer to become writable or the coroutine will be scheduled out because the tcp buffer is full?

1

There are 1 best solutions below

4
On

The read call is blocking, so yes, regardless of whether you are in a coroutine it blocks. This is documented: https://www.boost.org/doc/libs/1_82_0/doc/html/boost_asio/reference/read/overload1.html

"The call will block until one of the following conditions is true: [...]"

Now, if you're looking at simplifying async code with coroutines you can "just" substitute

 x = XYZ(a, b, c);

With

 x = co_await async_XYZ(a, b, c, asio::use_awaitable);

PRO TIP: Default completion tokens

You can bind the default completion token (use_awaitable here) to an executor, and use that with your IO object. See as_default_on and the examples that use it. Then your code can become

 x = co_await async_XYZ(a, b, c);