How to share dependencies in composed Asio operations
09:05 17 Sep 2025

This question nerd-sniped me by a problem mentioned in the comments:

@sehe use_promise seems very interesting. I looked up some documentation about it and found some of your posts, like this. But how could I imitate boost::wait_for_all with use_promise? There seems to be also a problem to have two tasks waiting for one single task to finish. – YzEdwin Commented yesterday

You can make a group of async operations (make_parallel_group) and then initiate it with use_promise. Incidentally, experimental::promise is especially versatile because the promise's operator() makes that an async operation as well, so it can be grouped/deferred in turn – sehe Commented yesterday

@sehe I finally got use_promise working with make_parallel_group. I was using asio::deferred before. I guess use_promise is more like eager evaluation? But still the biggest problem is forking. E.g. I have two independent tasks A and B, which are both relying on task C to finish. All three events should be executed only once. This could easily be done with boost::shared_future as I could just pass the shared_future of task C to the then() of task A and B. But if I use co_spawn and both tasks co_await task C, task C will be executed twice. – YzEdwin Commented 20 hours ago

The shared-dependency point is valid: asio::experimental::promise<...> is move-only and cannot be awaited twice. In that respect it's more like wrapped async operation (as per the concept check is_async_operation).

  • Can we wrap the promise into a copyable/shared promise type?

  • How would we do that?

  • Is the experimental::promise even required?

c++ promise dependencies boost-asio shared