Skip to content

Commit 6d37240

Browse files
authored
Update Task.cancel() with more details
Add more details into cancel() docs, some developers were worried if they are allowed to call it concurrently and multiple times etc.
1 parent 8fe0fd1 commit 6d37240

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

stdlib/public/Concurrency/Task.swift

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,18 +194,29 @@ extension Task {
194194
}
195195
}
196196

197-
/// Indicates that the task should stop running.
197+
/// Marks this task as cancelled.
198198
///
199199
/// Task cancellation is cooperative:
200200
/// a task that supports cancellation
201-
/// checks whether it has been canceled at various points during its work.
201+
/// checks whether it has been canceled at various points during its work,
202+
/// and may either return early or throw an error in order to
203+
/// finish running earlier than it would if it wasn't cancelled.
204+
///
205+
/// Calling this method only has the effect of setting the cancelled
206+
/// flag on the task (or its child tasks), and unless the tasks itself
207+
/// react on this flag, there is no observable runtime behavior change
208+
/// of the tasks. Specifically, tasks are never "interrupted and stopped"
209+
/// forcefully, and are always allowed to execute code until returning or throwing/
202210
///
203-
/// Calling this method on a task that doesn't support cancellation
204-
/// has no effect.
205211
/// Likewise, if the task has already run
206212
/// past the last point where it would stop early,
207213
/// calling this method has no effect.
208214
///
215+
/// It is safe to call `cancel` from any task or thread.
216+
/// The `cancel` call is idempotent, and may be called multiple times,
217+
/// and cancellation handlers will trigger only a single (first)
218+
/// time the task is cancelled.
219+
///
209220
/// - SeeAlso: `Task.checkCancellation()`
210221
public func cancel() {
211222
Builtin.cancelAsyncTask(_task)

0 commit comments

Comments
 (0)