Feedback request: Future cancellation #192
Draft
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.
In my project we would greatly benefit from having futures support cancellation requests.
The code in this PR is just a rough outline showing my approach, it is incomplete and untested. The goal is to have cancellation bridged between languages as well (ie: your swift/objc code can cancel a future from C++), but I have not implemented this yet.
The reason why I'm creating an incomplete PR is that I'd like early feedback (@li-feng-sc 🙏 ). Concretely:
djinni::Future
, or would you prefer a separate class, for exampleCancelableFuture
? Do you agree with my implementation approach so far?Examples
Cancellation forwarding:
Checking for cancellation
Notes
The implementation is based on
inplace_stop_token
, which is approved for C++26. This allows cancellation with a well defined interface and minimal overhead (no extra heap allocations etc). The token is stored in a futures SharedState.The overhead could probably be further reduced by replacing the mutex with atomics, but I've decided to go for a simple implementation first