Skip to content

Commit fce55dd

Browse files
adriansergheevAdrian Sergheev
andauthored
Task.yield() example (#82)
* Task.yield() example * rewrite --------- Co-authored-by: Adrian Sergheev <[email protected]>
1 parent b2b17f3 commit fce55dd

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

Guide.docc/IncrementalAdoption.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,3 +508,21 @@ await withTaskGroup(of: Something.self) { group in
508508
}
509509
}
510510
```
511+
512+
If the "work" task involves long-running synchronous code it may make sense to voluntarily suspend the task and allow other tasks to execute:
513+
514+
```swift
515+
struct Work {
516+
let dependency: Dependency
517+
func work() async {
518+
await dependency.fetch()
519+
// execute part of long running synchronous code
520+
await Task.yield() // explicitly insert a suspension point
521+
// continue long running synchronous execution
522+
}
523+
}
524+
```
525+
526+
Introducing an explicit suspension point helps Swift balance between making progress on this task, and letting other tasks in your program make progress on their work.
527+
However, if this task has the highest-priority in the system, the executor immediately resumes execution of the same task.
528+
Therefore an explicit suspension point isn’t necessarily a way to avoid resource starvation.

0 commit comments

Comments
 (0)