Sync: Types of which references can be shared between threads
Send: Types that can be transferred across thread boundaries
&T
is Send
if and only if T
is Sync
&mut T
is Send
if and only if T
is Send
&T
and &mut T
are Sync
if and only if T
is Sync
T
is Sync
if and only if &T
is Send
This statement is essentially a shorthand way of saying that if a type is thread-safe for shared use, it is also thread-safe to pass references of it across threads.This is because if a type is Sync it means that it can be shared across multiple threads without the risk of data races or other synchronization issues, so it is safe to move it to another thread. A reference to the type is also safe to move to another thread, because the data it references can be accessed from any thread safely.
这种说法本质上是一种简略的说法,即如果一个类型在共享使用时是线程安全的,那么跨线程传递它的引用也是线程安全的。这是因为如果一个类型是同步的,就意味着它可以跨多个线程共享,而不会有data race或其他同步问题的风险,因此将它移动到另一个线程是安全的。将该类型的引用转移到另一个线程也是安全的,因为可以从任何线程安全地访问它所引用的数据。
How to understand "primitive types are Sync" in rust?
Send + !Sync
These types can be moved to other threads, but they’re not thread-safe. Typically because of interior mutability:
mpsc::Sender<T>
mpsc::Receiver<T>
Cell<T>
RefCell<T>