cancellation_event and cancellation_token
cancellation_event is a type that is used to request cancellation of an
asynchronous operation.
cancellation_token is a type constructible from a cancellation_event that's
used by operations to check for cancellation.
Prototype
struct cancellation_event {
cancellation_event(const cancellation_event &) = delete;
cancellation_event(cancellation_event &&) = delete;
cancellation_event &operator=(const cancellation_event &) = delete;
cancellation_event &operator=(cancellation_event &&) = delete;
void cancel(); // (1)
[[nodiscard]] deferred_cancellation cancel_deferred(); // (2)
void cancel_deferred(deferred_cancellation &deferred); // (3)
void reset(); // (4)
};
struct cancellation_token {
cancellation_token(); // (5)
cancellation_token(cancellation_event &ev); // (6)
bool is_cancellation_requested(); // (7)
};
- Requests cancellation.
- Requests cancellation but does not invoke the callbacks; see deferred_cancellation.
- Same as (2), but adds the callbacks to an existing
deferred_cancellation. - Resets the object to prepare it for reuse.
- Default constructs a cancellation token.
- Constructs a cancellation token from a cancellation event.
- Checks whether cancellation is requested.
Requirements
reset()must only be called if external synchronization guarantees that nobody uses the event anymore. In particular, no callbacks may be attached to the event (or be pending in adeferred_cancellation) and no cancellation may be requested concurrently.
Arguments
deferred- thedeferred_cancellationthat receives the callbacks.ev- the cancellation event to use.
Return values
- This method doesn't return any value.
- Returns the callbacks that still need to be invoked.
- Same as (1).
- Same as (1).
- N/A
- N/A
- Returns
trueif cancellation was requested,falseotherwise.
Example
async::run([] () -> async::result<void> {
async::queue<int, frg::stl_allocator> q;
async::cancellation_event ce;
ce.cancel(); // Request cancellation immediately
auto v = q.async_get(ce);
if (!v)
std::cout << "Cancelled" << std::endl;
else
std::cout << "Completed: " << *v << std::endl;
}());
Output:
Cancelled