I was writing some asio code and tried to refactor it to use C++20 coroutines. However I got stuck transforming this code:
asio::post(
my_strand,
[self = shared_from_this()]() {
// functions that write in this container can only be called
// on a single thread at a time, thus the strand
session_write_history.push_back(buffer);
/* co_await? */ socket.write_async(buffer, /* use awaitable? */);
}
);
You see, my async operation is done inside the post callback, so using asio::use_awaitable
on the async operation one would make the callback a coroutine. Is there a way to await on the async operation inside the asio::post
on the strand?