1

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?

qing zhao
  • 23
  • 2

1 Answers1

0

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);
sehe
  • 374,641
  • 47
  • 450
  • 633
  • Edit: I forgot to insert async_ due to copy/paste... – sehe Aug 01 '23 at 20:20
  • golang encapsulates the network io system call and coroutines it if there is a block. Why doesn't asio do the same? – qing zhao Aug 02 '23 at 04:18
  • Because it doesn't assume you accept the overhead. For example you can use Asio without any threads. You can integrate it with another framework that supplies the event loop (eg a game engine) etc. As usual C++ favors genericity and low cost over convenience – sehe Aug 02 '23 at 13:24
  • By the way "golang coroutines it if there's a block" to me sounds like a confused way of saying it's nonblocking. I'm not well versed with golang to know whether there is some special hidden feature you are referring to, but I can't readily think of anything. – sehe Aug 02 '23 at 13:28