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)
void reset(); // (2)
};
struct cancellation_token {
cancellation_token(); // (3)
cancellation_token(cancellation_event &ev); // (4)
bool is_cancellation_requested(); // (5)
};
- Requests 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.
Arguments
ev
- the cancellation event to use.
Return values
- This method doesn't return any value.
- Same as (1).
- N/A
- N/A
- Returns
true
if cancellation was requested,false
otherwise.
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