Naios / continuable

C++14 asynchronous allocation aware futures (supporting then, exception handling, coroutines and connections)
https://naios.github.io/continuable/
MIT License
815 stars 44 forks source link

How to cancel a task based on timeout? #69

Open qiangxinglin opened 8 months ago

qiangxinglin commented 8 months ago

@Naios Thank you for the bravo project!

I want to implement a sync interface, suppose we have a response struct:

I have following code

auto task = asio::post(ioc, use_continuable).then([=]() { // where ioc is a asio::io_context running in a worker thread
    this_thread::sleep_for(300ms); // simulate some long task
    return 42;
})
auto res = std::move(task).apply(transforms::wait_for(50ms));
if (res.is_value())
    return response{success, *res};
else
    return response{fail, "timeout"}; // how to invalidate the task above?
Naios commented 6 months ago

Hi @qiangxinglin , you can use when_any and combine it with a timer. Continuable does not support real cancellation of continuations and you would have to implement this for your specific task.