Skip to content

Commit 911f0f9

Browse files
committed
update wording
1 parent 906d89f commit 911f0f9

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

stdlib/public/Concurrency/Task.swift

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ import Swift
7878
/// and additional reasons can accrue during the cancellation process.
7979
///
8080
/// ### Task closure lifetime
81-
/// A task is initialized with a closure. containing the code that the task executes.
81+
/// Tasks are initialized by passing a closure containing the code that will be executed by a given task.
8282
///
8383
/// After this code has run to completion, the task has completed, resulting in either
8484
/// a failure or result value, this closure is eagerly released.
@@ -88,7 +88,7 @@ import Swift
8888
/// after the task completes.
8989
/// Consequently, tasks rarely need to capture weak references to values.
9090
///
91-
/// For example, in this example, it is not necessary to capture the actor as weak,
91+
/// For example, in the following snippet of code it is not necessary to capture the actor as `weak`,
9292
/// because as the task completes it'll let go of the actor reference, breaking the
9393
/// reference cycle between the Task and the actor holding it.
9494
///
@@ -100,21 +100,42 @@ import Swift
100100
/// var result: Work?
101101
///
102102
/// deinit {
103-
/// print("deinit")
103+
/// assert(work != nil)
104+
/// // even though the task is still retained,
105+
/// // once it completes it no longer causes a reference cycle with the actor
106+
///
107+
/// print("deinit actor")
104108
/// }
105109
///
106110
/// func start() {
107111
/// work = Task {
112+
/// print("start task work")
108113
/// try? await Task.sleep(for: .seconds(3))
109-
/// self.work = Work() // we captured self
114+
/// self.result = Work() // we captured self
115+
/// print("completed task work")
110116
/// // but as the task completes, this reference is released
111117
/// }
118+
/// // we keep a strong reference to the task
112119
/// }
113120
/// }
121+
/// ```
122+
///
123+
/// And using it like this:
114124
///
125+
/// ```
115126
/// await Actor().start()
116-
/// // no references to Actor other than inside the Task
117-
/// // as the Task completes; the closure is destroyed releasing the actor.
127+
/// ```
128+
///
129+
/// Note that there is nothing, other than the Task's use of `self` retaining the actor,
130+
/// And that the start method immediately returns, without waiting for the unstructured `Task` to finish.
131+
/// So once the task completes and its the closure is destroyed, the strong reference to the "self" of the actor is also released allowing the actor to deinitialize as expected.
132+
///
133+
/// Therefore, the above call will consistently result in the following output:
134+
///
135+
/// ```
136+
/// start task work
137+
/// completed task work
138+
/// deinit actor
118139
/// ```
119140
@available(SwiftStdlib 5.1, *)
120141
@frozen

0 commit comments

Comments
 (0)