File tree Expand file tree Collapse file tree 1 file changed +11
-8
lines changed Expand file tree Collapse file tree 1 file changed +11
-8
lines changed Original file line number Diff line number Diff line change @@ -92,6 +92,8 @@ use tokio::{
92
92
/// ```
93
93
/// use tokio_util::sync::CancellationToken;
94
94
/// use tokio_util::task::TaskTracker;
95
+ /// use tokio_util::time::FutureExt;
96
+ ///
95
97
/// use tokio::time::{self, Duration};
96
98
///
97
99
/// async fn background_task(num: u64) {
@@ -111,17 +113,18 @@ use tokio::{
111
113
/// for i in 0..10 {
112
114
/// let token = token.clone();
113
115
/// tracker.spawn(async move {
114
- /// // Use a `tokio::select!` to kill the background task if the token is
115
- /// // cancelled.
116
- /// tokio::select! {
117
- /// () = background_task(i) => {
118
- /// println!("Task {} exiting normally.", i);
119
- /// },
120
- /// () = token.cancelled() => {
116
+ /// // Use a `with_cancellation_token_owned` to kill the background task
117
+ /// // if the token is cancelled.
118
+ /// match background_task(i)
119
+ /// .with_cancellation_token_owned(token)
120
+ /// .await
121
+ /// {
122
+ /// Some(()) => println!("Task {} exiting normally.", i),
123
+ /// None => {
121
124
/// // Do some cleanup before we really exit.
122
125
/// time::sleep(Duration::from_millis(50)).await;
123
126
/// println!("Task {} finished cleanup.", i);
124
- /// },
127
+ /// }
125
128
/// }
126
129
/// });
127
130
/// }
You can’t perform that action at this time.
0 commit comments