Skip to content

Commit f177aad

Browse files
authored
task: add JoinHandle::abort_handle (#5543)
1 parent 4ea6320 commit f177aad

File tree

1 file changed

+57
-24
lines changed

1 file changed

+57
-24
lines changed

tokio/src/runtime/task/join.rs

+57-24
Original file line numberDiff line numberDiff line change
@@ -182,28 +182,28 @@ impl<T> JoinHandle<T> {
182182
/// ```rust
183183
/// use tokio::time;
184184
///
185-
/// #[tokio::main]
186-
/// async fn main() {
187-
/// let mut handles = Vec::new();
188-
///
189-
/// handles.push(tokio::spawn(async {
190-
/// time::sleep(time::Duration::from_secs(10)).await;
191-
/// true
192-
/// }));
193-
///
194-
/// handles.push(tokio::spawn(async {
195-
/// time::sleep(time::Duration::from_secs(10)).await;
196-
/// false
197-
/// }));
198-
///
199-
/// for handle in &handles {
200-
/// handle.abort();
201-
/// }
202-
///
203-
/// for handle in handles {
204-
/// assert!(handle.await.unwrap_err().is_cancelled());
205-
/// }
185+
/// # #[tokio::main(flavor = "current_thread", start_paused = true)]
186+
/// # async fn main() {
187+
/// let mut handles = Vec::new();
188+
///
189+
/// handles.push(tokio::spawn(async {
190+
/// time::sleep(time::Duration::from_secs(10)).await;
191+
/// true
192+
/// }));
193+
///
194+
/// handles.push(tokio::spawn(async {
195+
/// time::sleep(time::Duration::from_secs(10)).await;
196+
/// false
197+
/// }));
198+
///
199+
/// for handle in &handles {
200+
/// handle.abort();
201+
/// }
202+
///
203+
/// for handle in handles {
204+
/// assert!(handle.await.unwrap_err().is_cancelled());
206205
/// }
206+
/// # }
207207
/// ```
208208
/// [cancelled]: method@super::error::JoinError::is_cancelled
209209
pub fn abort(&self) {
@@ -220,9 +220,8 @@ impl<T> JoinHandle<T> {
220220
/// ```rust
221221
/// use tokio::time;
222222
///
223-
/// # #[tokio::main(flavor = "current_thread")]
223+
/// # #[tokio::main(flavor = "current_thread", start_paused = true)]
224224
/// # async fn main() {
225-
/// # time::pause();
226225
/// let handle1 = tokio::spawn(async {
227226
/// // do some stuff here
228227
/// });
@@ -252,7 +251,41 @@ impl<T> JoinHandle<T> {
252251
}
253252

254253
/// Returns a new `AbortHandle` that can be used to remotely abort this task.
255-
pub(crate) fn abort_handle(&self) -> super::AbortHandle {
254+
///
255+
/// Awaiting a task cancelled by the `AbortHandle` might complete as usual if the task was
256+
/// already completed at the time it was cancelled, but most likely it
257+
/// will fail with a [cancelled] `JoinError`.
258+
///
259+
/// ```rust
260+
/// use tokio::{time, task};
261+
///
262+
/// # #[tokio::main(flavor = "current_thread", start_paused = true)]
263+
/// # async fn main() {
264+
/// let mut handles = Vec::new();
265+
///
266+
/// handles.push(tokio::spawn(async {
267+
/// time::sleep(time::Duration::from_secs(10)).await;
268+
/// true
269+
/// }));
270+
///
271+
/// handles.push(tokio::spawn(async {
272+
/// time::sleep(time::Duration::from_secs(10)).await;
273+
/// false
274+
/// }));
275+
///
276+
/// let abort_handles: Vec<task::AbortHandle> = handles.iter().map(|h| h.abort_handle()).collect();
277+
///
278+
/// for handle in abort_handles {
279+
/// handle.abort();
280+
/// }
281+
///
282+
/// for handle in handles {
283+
/// assert!(handle.await.unwrap_err().is_cancelled());
284+
/// }
285+
/// # }
286+
/// ```
287+
/// [cancelled]: method@super::error::JoinError::is_cancelled
288+
pub fn abort_handle(&self) -> super::AbortHandle {
256289
self.raw.ref_inc();
257290
super::AbortHandle::new(self.raw)
258291
}

0 commit comments

Comments
 (0)