On Monday, June 29th, 2026 at 5:01 PM, Rainer Deyke via Boost <boost@lists.boost.org> wrote:
The second escape hatch is immediate_executor. It looks something like this:
class immediate_executor { public: std::coroutine_handle<> ce.dispatch(capy::continuation &c) { // Obey the letter of the law by not just returning c.h... this->post(c); return {}; }
void post(capy::continuation &c) { // ...but violate the spirit of the law by calling h.resume(). c.h.resume(); } // ...other functions here... };
immediate_executor is a powerful way for opting out of costs, and the benefits, of more robust executors where they are not needed or wanted. And it appears to be completely legal. It works great in conjunction with resume_on, but it also reduces the cost of capy::run significantly, since switching to an immediate_executor is effective free.
Put big red warning signs on immediate_executor that it should only be used by experts who know what they are doing if you have to, but provide it.
As written I don't think it's viable. Calling resume() from post adds stack frames and introduces stack growth, which we deliberately avoid.
Yes, there's a risk of stack overflow. It can be mitigated by returning the coroutine handle from dispatch instead of calling resume, but it can't be eliminated entirely. This is definitely not a very safe, or otherwise very good, general purpose executor. It's an escape hatch for high performance code where the user really knows what he's doing.
The inline behavior already exists. When an executor is safe to run inline (already on the right thread), its dispatch returns the continuation handle for symmetric transfer.
As you said, only in contexts where you're already on the right thread.
Consider this example again:
task<int> f() { capy::run(ex1)([] { do_something_on_e1(); }); capy::run(ex2)([] { do_something_on_e2(); }); capy::run(ex3)([] { do_something_on_e3(); }); }
Running this on an immediate_executor effectively eliminates three of the six executor hops, and the amount of time it spends on the wrong thread is negligible.
This use of immediate_executor seems wasteful. If ex1, ex2, ex3 are all immediate executors, then run is adding trampolines and executor hops that don't go anywhere meaningful. The work runs on the current thread regardless. So my first instinct is just do the following: task<int> f() { do_something_on_e1(); do_something_on_e2(); do_something_on_e3(); } which is more efficient than the immediate_executor version and runs in exactly the same place. The only thing it leaves out is a suspension point between the stages. If that seam is the actual goal (scheduler can run in between, cancellation can be respected), then I think the right tool is a yield: task<int> f() { do_something_on_e1(); co_await capy::this_coro::yield; do_something_on_e2(); co_await capy::this_coro::yield; do_something_on_e3(); } This is just a tiny awaitable that allows the scheduler to regain control. Lower overhead overall. We have this already but it is not public, maybe this is a case to make it so.