Description
The difficult part of designing when_any() will be how to handle cancellation of the co_await operations of the other tasks.
Currently, the task<T>
and shared_task<T>
types don't allow the caller to cancel the co_await
operation once it has been awaited. We need to wait for the task to complete before the awaiting coroutine returns.
If the tasks themselves are cancellable, we could hook something up using cancellation_token
s.
eg. If we pass the same cancellation_token
into each task then concurrently await all of the tasks and when any task completes, we then call request_cancellation()
on the cancellation_source
to request the other tasks to cancel promptly. Then we could just use when_all()
to wait for all of the tasks.
To do this more generally, we'd need to be able to cancel the await operation on a task without necessarily cancelling the task itself. This would require a different data-structure in the promise object for keeping track of awaiters to allow unsubscribing an awaiter from that list in a lock-free way.
Maybe consider a similar data-structure to that used by cancellation_registration
?