Rust Reference ยท Core Traits
The Send and Sync traits
Reference for Rust Send and Sync: what the auto traits promise, which std types lack them (Rc, RefCell, raw pointers), manual unsafe impls and common compiler errors.
Send and Sync are marker traits with no methods. They tell the compiler what is safe to do with a type across threads. A type is Send if ownership of a value can be moved to another thread. A type is Sync if a shared reference &T can be used from several threads at once; precisely, T: Sync exactly when &T: Send.
Both are auto traits: the compiler implements them for a struct or enum automatically when all its fields have them. This is how Rust prevents data races at compile time. thread::spawn requires Send for the closure and everything it captures.
fn needs_send<T: Send>(_: &T) {}
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Syntax
pub unsafe auto trait Send {} pub unsafe auto trait Sync {} // Bounds fn spawn_it<F: FnOnce() + Send + 'static>(f: F); Box<dyn Fn() + Send + Sync> // Opting in manually (unsafe) and opting out (nightly only) unsafe impl Send for MyPtr {} unsafe impl Sync for MyPtr {}
| Trait | Meaning | Example |
|---|---|---|
Send |
The value may be moved to another thread. | Vec<T>, Arc<T>; MutexGuard is not |
Sync |
&T may be shared between threads. |
Mutex<T>, atomics; Cell and RefCell are not |
| neither | Confined to one thread. | Rc<T>, raw pointers |
Errors / panics / edge cases: the usual error is "Rc<i32> cannot be sent between threads safely" (E0277). A wrong manual unsafe impl causes undefined behavior, not a compile error.
Usage notes
- They propagate. A struct is
Sendonly if every field is, so oneRcor raw pointer inside turns the whole type non-Send. This is why the error often points at a field deep in a type. - Typical combinations.
Arc<T>isSend + SyncifT: Send + Sync.Mutex<T>isSyncifT: Send.RwLock<T>isSyncifT: Send + Sync.Arc<RefCell<T>>is notSendbecauseRefCell<T>is notSync. Sendbut notSync:Cell,RefCell,mpsc::Receiver. Move them into one thread, but do not share a reference.Syncbut notSend: rare, for example aMutexGuardon some platforms (it must be unlocked by the thread that locked it).- Raw pointers are neither, so a struct containing
*mut Topts out automatically. A safe wrapper that really is thread-safe can addunsafe impl Sendwith a comment explaining why. 'staticis separate.thread::spawnneedsSend + 'static; scoped threads (thread::scope) need onlySendsince borrows are allowed.- Async code hits the same rule: a future held across an
.awaitmust beSendfor multi-threaded executors, so avoid keepingRcorRefCellborrows alive across awaits. - Not a runtime check. These traits exist only at compile time and cost nothing.
Related
dyn,Rc<T>,Fn,FnMutandFnOnce,Sized- Lessons: Send and Sync, Threads, Mutex and RwLock