My application use boost.asio to run network related tasks that yield (coroutines).
On some occasions when my task is spawned from another spawned task, it's being called directly from the current task's callstack.
I would expect that it'll be queued and may run after the current task get suspended (if no other tasks are pending on the same io_context)
looking at the spawn code, it reaches boost::asio::dispatch
which trigger async operation
return async_initiate<NullaryToken, void()>(
detail::initiate_dispatch(), token);
So it seems odd that my task has been called directly and not from some dispatcher, is it possible ?
Thanks
asio::dispatch
is documented to dispatch on an executor.
The Executor
concept documents the behaviour for dispatch
as:
Effects: Creates an object f1 initialized with
DECAY_COPY(forward<Func>(f))
(C++Std [thread.decaycopy
]) in the current thread of execution . Callsf1()
at most once. The executor may block forward progress of the caller untilf1()
finishes execution.
(emphasis mine).
Contrast this with post
or defer
:
The executor shall not block forward progress of the caller pending completion of
f1()
Simply put: dispatch
MAY execute the handler immediately but only if the currently executing executor implements this optimization and is compatible with the associated executor.
See also for background/examples: