0
0
Swiftprogramming~10 mins

Sendable protocol for thread safety in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Sendable protocol for thread safety
Define a type
Conform to Sendable
Use in concurrent code
Compiler checks thread safety
Safe data sharing
Avoid data races
The Sendable protocol marks types as safe to share across threads. The compiler checks this to prevent data races.
Execution Sample
Swift
struct Counter: Sendable {
    var value: Int
}

func update(counter: inout Counter) {
    counter.value += 1
}
Defines a thread-safe Counter struct conforming to Sendable and updates it asynchronously.
Execution Table
StepActionEvaluationResult
1Define struct Counter with Int valueCounter is SendableCounter marked safe for concurrency
2Call update(counter:) asynchronouslycounter.value = 0 initiallyPrepare to increment
3Increment counter.value by 1counter.value was 0counter.value becomes 1
4Function completesNo data race detectedSafe update confirmed
5ExitAll operations thread-safeExecution ends
💡 Function completes with thread-safe update, no data races
Variable Tracker
VariableStartAfter 1After 2Final
counter.value0011
Key Moments - 3 Insights
Why must Counter conform to Sendable?
Because the compiler uses Sendable to check if Counter can be safely shared across threads, as shown in step 1 of the execution_table.
What happens if Counter has a non-Sendable property?
The compiler will give an error during the check in step 1, preventing unsafe concurrent use.
Why is incrementing counter.value safe here?
Because the function runs asynchronously but the type is Sendable, ensuring no data races as seen in steps 3 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is counter.value after step 3?
A1
B0
CUndefined
D2
💡 Hint
Check the 'Result' column at step 3 in the execution_table.
At which step does the compiler confirm thread safety?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the 'Evaluation' column in step 1 where Sendable conformance is checked.
If Counter had a non-Sendable property, what would change in the execution_table?
AStep 3 would increment twice
BStep 1 would show a compiler error
CStep 4 would show data race detected
DNo change
💡 Hint
Refer to key_moments about compiler checks at step 1.
Concept Snapshot
Sendable protocol marks types safe for concurrency.
Conform your types to Sendable to share across threads.
Compiler checks Sendable conformance to prevent data races.
Use Sendable types in async or concurrent code safely.
Non-Sendable properties cause compile errors.
Always prefer immutable or thread-safe data for concurrency.
Full Transcript
The Sendable protocol in Swift is used to mark types that can be safely shared across threads. When you define a type like a struct, you can conform it to Sendable. The compiler then checks if all properties are also Sendable. This ensures no data races happen when the type is used in concurrent or asynchronous code. For example, a Counter struct with an Int value can conform to Sendable. When you update this counter asynchronously, the compiler confirms thread safety. If the type had a non-Sendable property, the compiler would give an error. This helps keep your code safe and free from hard-to-find bugs caused by concurrent access.