Questions about Send and Sync traits
⚓ Rust 📅 2026-02-26 👤 surdeus 👁️ 3I am going through Rust atomics and locks book where it builds spin locks.
Here is the structure:
struct SpinLock<T> {
locked: AtomicBool,
value: UnsafeCell<T>,
}
unsafe impl<T> Sync for SpinLock<T> where T: Send {}
Now I see the documentation says T is Send if it can be safely moved across thread and T is Sync if &T can be shared with threads concurrently.
It requires T be Send only and not Sync. Why?
but below requires it to be Send + Sync -
struct RwLock<T> {
state: AtomicU32,
value: UnsafeCell<T>,
}
unsafe impl<T> Sync for RwLock<T> where T: Send + Sync {}
Could anyone please explain with an example how these two traits change the behavior of implementers?
8 posts - 5 participants
🏷️ Rust_feed