Skip to content

Commit dc22978

Browse files
committed
correct documentation about what cancellation means
1 parent 59719fb commit dc22978

File tree

2 files changed

+47
-11
lines changed

2 files changed

+47
-11
lines changed

stdlib/public/Concurrency/DiscardingTaskGroup.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,15 +547,46 @@ public struct ThrowingDiscardingTaskGroup<Failure: Error> {
547547
#endif
548548
}
549549

550+
/// A Boolean value that indicates whether the group has any remaining tasks.
550551
///
552+
/// At the start of the body of a `withThrowingDiscardingTaskGroup(of:returning:body:)` call,
553+
/// the task group is always empty.
554+
///
555+
/// It's guaranteed to be empty when returning from that body
556+
/// because a task group waits for all child tasks to complete before returning.
557+
///
558+
/// - Returns: `true` if the group has no pending tasks; otherwise `false`.
551559
public var isEmpty: Bool {
552560
_taskGroupIsEmpty(_group)
553561
}
554562

563+
/// Cancel all of the remaining tasks in the group.
564+
///
565+
/// If you add a task to a group after canceling the group,
566+
/// that task is canceled immediately after being added to the group.
567+
///
568+
/// Immediately cancelled child tasks should therefore cooperatively check for and
569+
/// react to cancellation, e.g. by throwing an `CancellationError` at their
570+
/// earliest convenience, or otherwise handling the cancellation.
571+
///
572+
/// There are no restrictions on where you can call this method.
573+
/// Code inside a child task or even another task can cancel a group,
574+
/// however one should be very careful to not keep a reference to the
575+
/// group longer than the `with...TaskGroup(...) { ... }` method body is executing.
576+
///
577+
/// - SeeAlso: `Task.isCancelled`
578+
/// - SeeAlso: `ThrowingTaskGroup.isCancelled`
555579
public func cancelAll() {
556580
_taskGroupCancelAll(group: _group)
557581
}
558582

583+
/// A Boolean value that indicates whether the group was canceled.
584+
///
585+
/// To cancel a group, call the `ThrowingDiscardingTaskGroup.cancelAll()` method.
586+
///
587+
/// If the task that's currently running this group is canceled,
588+
/// the group is also implicitly canceled,
589+
/// which is also reflected in this property's value.
559590
public var isCancelled: Bool {
560591
return _taskGroupIsCancelled(group: _group)
561592
}

stdlib/public/Concurrency/TaskGroup.swift

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -488,15 +488,17 @@ public struct TaskGroup<ChildTaskResult: Sendable> {
488488

489489
/// Cancel all of the remaining tasks in the group.
490490
///
491-
/// After cancellation,
492-
/// any new results from the tasks in this group
493-
/// are silently discarded.
494-
///
495491
/// If you add a task to a group after canceling the group,
496492
/// that task is canceled immediately after being added to the group.
497493
///
498-
/// This method can only be called by the parent task that created the task
499-
/// group.
494+
/// Immediately cancelled child tasks should therefore cooperatively check for and
495+
/// react to cancellation, e.g. by throwing an `CancellationError` at their
496+
/// earliest convenience, or otherwise handling the cancellation.
497+
///
498+
/// There are no restrictions on where you can call this method.
499+
/// Code inside a child task or even another task can cancel a group,
500+
/// however one should be very careful to not keep a reference to the
501+
/// group longer than the `with...TaskGroup(...) { ... }` method body is executing.
500502
///
501503
/// - SeeAlso: `Task.isCancelled`
502504
/// - SeeAlso: `TaskGroup.isCancelled`
@@ -929,6 +931,7 @@ public struct ThrowingTaskGroup<ChildTaskResult: Sendable, Failure: Error> {
929931
///
930932
/// At the start of the body of a `withThrowingTaskGroup(of:returning:body:)` call,
931933
/// the task group is always empty.
934+
///
932935
/// It's guaranteed to be empty when returning from that body
933936
/// because a task group waits for all child tasks to complete before returning.
934937
///
@@ -939,15 +942,17 @@ public struct ThrowingTaskGroup<ChildTaskResult: Sendable, Failure: Error> {
939942

940943
/// Cancel all of the remaining tasks in the group.
941944
///
942-
/// After cancellation,
943-
/// any new results or errors from the tasks in this group
944-
/// are silently discarded.
945-
///
946945
/// If you add a task to a group after canceling the group,
947946
/// that task is canceled immediately after being added to the group.
948947
///
948+
/// Immediately cancelled child tasks should therefore cooperatively check for and
949+
/// react to cancellation, e.g. by throwing an `CancellationError` at their
950+
/// earliest convenience, or otherwise handling the cancellation.
951+
///
949952
/// There are no restrictions on where you can call this method.
950-
/// Code inside a child task or even another task can cancel a group.
953+
/// Code inside a child task or even another task can cancel a group,
954+
/// however one should be very careful to not keep a reference to the
955+
/// group longer than the `with...TaskGroup(...) { ... }` method body is executing.
951956
///
952957
/// - SeeAlso: `Task.isCancelled`
953958
/// - SeeAlso: `ThrowingTaskGroup.isCancelled`

0 commit comments

Comments
 (0)