Send and Sync¶
Category: Safety in languages · Status: stub · Lessons: chapter 02, Shared state
One line: Rust's two marker traits: a Send value may move to another thread, a Sync value may be shared with one by reference, and the compiler checks both.
Also called: Sendable.
How it connects¶
flowchart LR
n_data_race["Data race"]
n_data_race_freedom["Data-race freedom by construction"]
n_send_and_sync["Send and Sync"]
n_data_race_freedom -->|uses| n_send_and_sync
n_send_and_sync -->|prevents| n_data_race
classDef center stroke-width:3px
class n_send_and_sync center
classDef outside stroke-dasharray: 4 3
class n_data_race,n_data_race_freedom outside
- Is used by: Data-race freedom by construction
- Helps prevent: Data race
In each language¶
| Rust | Send ↗ and Sync ↗ are unsafe auto traits; Rc is not Send because its reference count is not atomic, while Arc is |
| Swift | Sendable ↗ plays the same part: a type whose values can cross concurrent contexts without risk of data races |
Where to read more¶
- In a sibling library: Rust: Send and Sync ↗
- In a sibling library: Rust: Sharing across threads: Arc ↗
- In the books: Hands-On Concurrency with Rust, Brian L. Troutwine — ch. 4, 'Sync and Send – the Foundation of Rust Concurrency'
- In the books: Rust Atomics and Locks, Mara Bos — ch. 1, 'Basics of Rust Concurrency' → 'Thread Safety: Send and Sync'
- In the books: The Rust Programming Language, Steve Klabnik, Carol Nichols — ch. 16, 'Fearless Concurrency' → 'Extensible Concurrency with the Sync and Send Traits'
- Reference: The Rustonomicon: Send and Sync ↗