Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to cancel a task based on timeout? #69

Open
qiangxinglin opened this issue Nov 1, 2023 · 1 comment
Open

How to cancel a task based on timeout? #69

qiangxinglin opened this issue Nov 1, 2023 · 1 comment

Comments

@qiangxinglin
Copy link

qiangxinglin commented Nov 1, 2023

@Naios Thank you for the bravo project!

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

  • Post a task to the executor
  • If it can be finished in the timeout, then retrieve the result, and return response{success, result}.
  • If it takes longer than the timeout, return response(fail, "timeout"), and set the running continuable to fail status.

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
Copy link
Owner

Naios commented Dec 14, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants