Rust Systems & Tokio (50 Qs)•Part 3: Advanced Internals, Scale & Production Patterns (Senior / Lead)
What are the `Send` and `Sync` marker traits in Rust Concurrency?
senior LevelrustAdvanced Internals
In Simple Words: The foundation of Rust's "Fearless Concurrency":
Send: Indicates that ownership of the type can be safely transferred across thread boundaries.- Almost all types are
Send. Exception:Rc<T>is NOTSendbecause non-atomic reference count updates cause race conditions!
- Almost all types are
Sync: Indicates that it is safe to share references to the type across multiple threads (&TisSend).- A type is
Syncif and only if&Tis thread-safe.RefCell<T>is NOTSync;Mutex<T>isSync.
- A type is
- Automatically implemented by the compiler if all struct members are
SendandSync.