I am trying out the C++20 coroutines with boost asio. My current intention is to embed in the coroutine example from https://www.boost.org/doc/libs/1_75_0/doc/html/boost_asio/example/cpp17/coroutines_ts/echo_server.cpp a simple suspension point.
As far as I understand the documentation here https://en.cppreference.com/w/cpp/coroutine/suspend_always, this call shall be valid:
co_await suspend_always{};
inside this coroutine:
awaitable<void> echo(tcp::socket socket)
{
try
{
char data[1024];
for (;;)
{
std::size_t n = co_await socket.async_read_some(boost::asio::buffer(data), use_awaitable);
co_await suspend_always{}; // <-- here
co_await async_write(socket, boost::asio::buffer(data, n), use_awaitable);
}
}
catch (std::exception& e)
{
std::printf("echo Exception: %s\n", e.what());
}
}
However, there is a compiler error:
error: no matching member function for call to 'await_transform'
co_await suspend_always{};
^~~~~~~~
Can someone please explain, how to introduce a suspension point into the above coroutine without using a timer with an async_wait
.