These are chat archives for rust-lang/rust

24th
Dec 2016
mhsjlw
@mhsjlw
Dec 24 2016 16:11
JumpNetwork freenode
oops :(
Matanel Levi
@matanelevi
Dec 24 2016 21:41
Anyone can please explain what are the differences between Sync and Send?
When each one is necessary? in what situations?
Aleksey Kladov
@matklad
Dec 24 2016 21:44
It's a bit tricky. Send is something you can move into another thread (via thread::spawn, for example). Sync is something that can be shared by reference (via crossbeam::scope, for example).
A good example of Send is Vec<u8>: although accessing Vec from two threads concurrently is not save, Vec can cross boundaries between threads if you completely move it from one thread into another.
A good example of !Send is Rc<u8> : if you clone Rc and send it to another thread, than two threads will be able to concurrently update non atomic reference count.
Also, here is a nice explanation: http://huonw.github.io/blog/2015/02/some-notes-on-send-and-sync/, have you already seen it?
Aleksey Kladov
@matklad
Dec 24 2016 21:55
A nice example of type which is Send but not !Sync is Cell<u8>: if you share a reference between two threads, then they can concurrently mutate the contents of the Cell, so this is disallowed. However, if you transfer the ownership from one thread to another then the race becomes impossible.
I don't know a type which is !Send but Sync.
Peter Atashian
@retep998
Dec 24 2016 23:29
@matklad Certain types must be destroyed on the same thread that they are created on, in which case they could be Sync yet !Send