Add C++/WinRT when_any async helper #524
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Complements #516 (and improves upon #520) by adding a
when_any
helper function that will complete when any of the supplied async objects have completed. The resulting async object can then either be waited upon usingco_await when_any(...)
orwhen_any(...).get()
. In addition, if the async objects areIAsyncOperation
orIAsyncOperationWithProgress
then the async object created bywhen_any
will also return the result of the first async object to complete.This is a little harder to implement than
when_all
, but just as useful for things like redundant operations or cooperative timeouts. As I mentioned in #341, WinRT only allows waiting once. That means that any objects that may not yet have completed may not be waited on again in any way.Here's an example:
And this program produces the following output:
Another example is to use it to implement a cooperative timeout:
For example:
And this program will print 42 if it is able to compute it within 2 seconds. Otherwise it prints 0.