Sub topic 5
How Sync Is Defined in Terms of Send
This chapter assumes you know ownership and borrowing, trait bounds, closures with move, and basic use of thread::spawn. It also assumes you know that Send means a value can be moved to another thread.
The rule
Sync has a precise definition: a type T is Sync exactly when &T is Send.
Read it as a question. Can a shared reference to this value be moved to another thread? If yes, the type is Sync. Several threads can then hold &T at once, and all of them are safe.
This explains why Sync is not a separate idea. Rust already had a rule for moving values between threads, which is Send. Sharing a value means moving a reference to it. So Sync is Send applied to &T.
Checking it with the compiler
You can test the equivalence with the helper functions from the previous chapters. Each call compiles only if the bound holds.
use std::cell::Cell;
use std::sync::Mutex;
fn assert_send<T: Send>() {}
fn assert_sync<T: Sync>() {}
fn main() {
// Mutex<i32> is Sync, so &Mutex<i32> is Send.
assert_sync::<Mutex<i32>>();
assert_send::<&Mutex<i32>>();
// Cell<i32> is not Sync, so &Cell<i32> is not Send.
// Each of these lines would fail to compile if uncommented:
// assert_sync::<Cell<i32>>();
// assert_send::<&Cell<i32>>();
// Cell<i32> itself is still Send.
assert_send::<Cell<i32>>();
}The two commented lines fail together. They ask the same question in two forms.
Why the definition makes sense
Take Cell<i32>. It lets you change the value through a shared reference, with no locking. If two threads each held a &Cell<i32>, both could write at the same moment. That is a data race. So &Cell<i32> must not be Send, which means Cell<i32> is not Sync.
Moving the Cell itself to one other thread is fine. Only one thread owns it afterward. This is why a type can be Send without being Sync.
Mutex<i32> is the opposite case. Every access goes through lock(), which lets one thread in at a time. A shared reference is safe to hand out, so Mutex<i32> is Sync.
Consequences you will meet
The definition gives you the four combinations of the two traits.
| Send | Sync | Example | Meaning |
|---|---|---|---|
| Yes | Yes | `i32`, `String`, `Mutex<i32>` | Move it or share it freely |
| Yes | No | `Cell<i32>`, `RefCell<i32>` | Move to one thread, but do not share |
| No | Yes | `MutexGuard<'_, T>` when `T: Sync` | Share it, but do not move it |
| No | No | `Rc<i32>` | Stay on one thread |
The third row is rare. A MutexGuard must be released on the thread that locked it on some platforms, so it is not Send. A &MutexGuard<T> only exposes &T, so the guard is Sync whenever T is.
A second consequence concerns Arc<T>. Cloning an Arc and sending the clone to another thread moves a handle, but both threads then reach the same T through shared references. So Arc<T> is Send only if T is both Send and Sync. The next chapters return to this bound.
Common mistakes
- Assuming
SendimpliesSync.CellandRefCellshow that it does not. - Assuming
SyncimpliesSend.MutexGuardshows that it does not. - Reading
T: Syncas "Tcan be moved." That isSend.Syncis about&T.
Exercise
RefCell<Vec<i32>> is Send but not Sync. Using the definition of Sync, explain why &RefCell<Vec<i32>> cannot be sent to another thread, and name one thing you could wrap it in so that threads can share it.
Show answer
Sync means &T is Send. RefCell<Vec<i32>> is not Sync, so &RefCell<Vec<i32>> is not Send. Two threads with such a reference could call borrow_mut at the same time, and RefCell's borrow counter is not atomic, so this would be a data race. Wrapping the Vec in a Mutex (Mutex<Vec<i32>>) makes it Sync, because lock() allows one thread at a time. An Arc<Mutex<Vec<i32>>> then lets each thread own a handle.