publish: true
tags:
- cheat-sheetThis page is intended to be a quick, glanceable view of Swift's broad concurrency features. It holds a list of Code: language level keywords, protocols, attributes, types and methods. There's also a list of Concepts which are necessary to understand the code.
See: Explore structured concurrency in Swift | WWDC '21 from 0:00 to 3:33
See: What is "structure"?
By default, Swift uses Structured Concurrency, however you can opt into Unstructured Concurrency.
See docs: Unstructured Concurrency | Swift Documentation
tl;dr; Swift provides various tools and features designed to guarantee, at compile-time, that our concurrent code is safe.
@Sendable attribute can be applied to closures, and propertiesasync keyword async can call other async functions and must be awaited.await keyword actor, await explicitly states Actor Reentrancy.Sendable protocol Sendable ensure that instances can be used safely in concurrent contexts.@Sendable attribute @unchecked Sendable Sendable, bypassing the compiler's automatic checks.@retroactive @unchecked Sendable Sendable in a module that does not own the type, using unchecked conformance.Sendable conformance.@preconcurrency attribute nonisolatedkeyword nonisolated(unsafe) keyword nonisolated, but used when the method might access isolated state unsafely.isolated keyword actor keyword Actor protocol. Actor protocol Actor protocol[1]The Actor protocol provides methods to inspect isolation:
GlobalActor protocol Many will say that a GlobalActor is just like a thread or a queue (from GCD). This is not quite correct. Be aware that there are subtle differences.
@globalActor attribute MainActor Mutex type Mutex allowing for exclusive access."sending keyword Task async functions and managed with task groups or task priorities.withCheckedContinuation method In the past, it was common for programmers to make heavy use of keywords like goto. This meant that any piece of code could be called from anywhere. This style of programming can be very powerful, but it can be incredibly difficult to read and understand. So programming languages added structure through concepts like scope and keywords like for.
Just like how every class, implicitly conforms to AnyObject↩︎