rust concurrency

Send and Sync

  • A type is Send if it is safe to send it to another thread.
  • A type is Sync if it is safe to share between threads (T is Sync if and only if &T is Send).
  • raw pointers are neither Send nor Sync (because they have no safety guards).
  • UnsafeCell isn’t Sync (and therefore Cell and RefCell aren’t).
  • Rc isn’t Send or Sync (because the refcount is shared and unsynchronized).

Types that aren’t automatically derived can simply implement them if desired:

1
2
3
4
struct MyBox(*mut u8);

unsafe impl Send for MyBox {}
unsafe impl Sync for MyBox {}

one can also unimplement Send and Sync:

1
2
3
4
5
6
7
#![feature(negative_impls)]

// I have some magic semantics for some synchronization primitive!
struct SpecialThreadToken(u8);

impl !Send for SpecialThreadToken {}
impl !Sync for SpecialThreadToken {}

reference