I have several time-consuming computation jobs, which shall not block the executing thread while executing.
I also want to use c++20 coroutines + asio::awaitable's to accomplish this.
The asio::io_context's thread for example should still be responsive.
Therefore, I want my Job to suspend/yield after some time, and then continue its own execution, when the executioner does not have other jobs to do. Also, here are other coroutines with want to await the result of the computations (1:n relation!).
What is the best (nearly no overhead due to timers for example) way to achieve this with boost::asio? Other coro libraries support something like events.
Here is a code snippet to show my intention:
MyReturnType result;
CoroEvent coro_event;
auto long_func(asio::ExecutionCtx ctx) -> awaitable<void> {
for (size_t i{}; i < 1000){
for (size_t j{}; i < 1000){
result.compute(i*1000+j);
}
ctx.yield();
}
coro_event.send();
co_return;
}
auto get_job_value() -> awaitable<MyReturnType> {
co_await coro_event.async_wait(use_awaitable); // continues if done, suspends if not.
co_return result;
}