Skip to content

Commit

Permalink
sync: add JoinHandle::abort (#2474)
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed Sep 9, 2020
1 parent a0a3561 commit cbb14a7
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions tokio/src/runtime/task/join.rs
Expand Up @@ -157,6 +157,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

0 comments on commit cbb14a7

Please sign in to comment.