@@ -182,28 +182,28 @@ impl<T> JoinHandle<T> {
182
182
/// ```rust
183
183
/// use tokio::time;
184
184
///
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());
206
205
/// }
206
+ /// # }
207
207
/// ```
208
208
/// [cancelled]: method@super::error::JoinError::is_cancelled
209
209
pub fn abort ( & self ) {
@@ -220,9 +220,8 @@ impl<T> JoinHandle<T> {
220
220
/// ```rust
221
221
/// use tokio::time;
222
222
///
223
- /// # #[tokio::main(flavor = "current_thread")]
223
+ /// # #[tokio::main(flavor = "current_thread", start_paused = true )]
224
224
/// # async fn main() {
225
- /// # time::pause();
226
225
/// let handle1 = tokio::spawn(async {
227
226
/// // do some stuff here
228
227
/// });
@@ -252,7 +251,41 @@ impl<T> JoinHandle<T> {
252
251
}
253
252
254
253
/// 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 {
256
289
self . raw . ref_inc ( ) ;
257
290
super :: AbortHandle :: new ( self . raw )
258
291
}
0 commit comments