I would like to learn how to pass timeout timer to boost::asio::yield_context
.
Let's say, in terms of Boost 1.80
, there is smth like the following:
#include <boost/asio/io_context.hpp>
#include <boost/asio/spawn.hpp>
void async_func_0(boost::asio::yield_context yield) {
async_func_1(yield);
}
void async_func_1(boost::asio::yield_context) {
}
int main() {
boost::asio::io_context ioc;
boost::asio::spawn(ioc.get_executor(), &async_func_0);
ioc.run();
return 0;
}
Let's imaging that the async_func_1
is quite a burden, it is async
by means of boost::coroutines
(since boost::asio
does not use boost::coroutines2
for some unknown reason) and it can work unpredictably long, mostly on io
operations.
A good idea would be to specify the call of async_func_1
with a timeout
so that if the time passed it must return whatever with an error. Let's say at the nearest use of boost::asio::yield_context
within the async_func_1
.
But I'm puzzled how it should be expressed in terms of boost::asio
.
P.S. Just to exemplify, in Rust
it would be smth like the following:
use std::time::Duration;
use futures_time::FutureExt;
async fn func_0() {
func_1().timeout(Duration::from_secs(60)).await;
}
async fn func_1() {
}
#[tokio::main]
async fn main() {
tokio::task::spawn(func_0());
}
In Asio cancellation and executors are separate concerns.
That's flexible. It also means you have to code your own timeout.
One very rough idea:
No online compilers that accept this¹ are able to run this currently. So here's local output:
And the handler visualizations:
¹ wandbox, coliru, CE
Road From Here
You'll probably say this is cumbersome. Compared to your Rust library feature it is. To library this in Asio you could
yield_context
, adding the behaviour you wantdeferred
)