Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: implement cancellation and raw handles #2474

Merged
merged 1 commit into from Sep 9, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 38 additions & 0 deletions tokio/src/runtime/task/join.rs
Expand Up @@ -91,6 +91,44 @@ impl<T> JoinHandle<T> {
_p: PhantomData,
}
}

/// Abort the task associated with the handle.
///
/// Awaiting a cancelled task might complete as usual if the task was
/// already completed at the time it was cancelled, but most likely it
/// will complete with a `Err(JoinError::Cancelled)`.
///
/// ```rust
/// use tokio::time;
///
/// #[tokio::main]
/// async fn main() {
/// let mut handles = Vec::new();
///
/// handles.push(tokio::spawn(async {
/// time::delay_for(time::Duration::from_secs(10)).await;
/// true
/// }));
///
/// handles.push(tokio::spawn(async {
/// time::delay_for(time::Duration::from_secs(10)).await;
/// false
/// }));
///
/// for handle in &handles {
/// handle.abort();
/// }
///
/// for handle in handles {
/// assert!(handle.await.unwrap_err().is_cancelled());
/// }
/// }
/// ```
pub fn abort(&self) {
if let Some(raw) = self.raw {
raw.shutdown();
}
}
}

impl<T> Unpin for JoinHandle<T> {}
Expand Down