These are chat archives for rust-lang/rust

26th
Dec 2016
Peter Atashian
@retep998
Dec 26 2016 01:13
@matanelevi Cell and Rc provide entirely different functionality.
@matanelevi Sync means that given two &T that point to the same thing, you can put them on different threads and work with them concurrently without issue
For most types &T is simply immutable so they are Sync and everything is fine
However Cell allows you to mutate the contents given nothing more than a &T and it does so without any sort of synchronization, so that would not be thread safe thus it is not Sync
Mutex provides similar functionality to Cell, but it does so with a mutex to make sure only one thread has access at a time, thus preserving thread safety so it can be Sync
Evgeniy Zheltonozhskiy
@Randl
Dec 26 2016 11:56
Someone can help me to fix rust-lang/rust#38441 ? I have a basic idea of what should be done, but I'm not fluent enough with both rust and the structure of files to do that
Matanel Levi
@matanelevi
Dec 26 2016 16:19
@retep998 I'm trying to say that I dont get why Send exists
Aleksey Kladov
@matklad
Dec 26 2016 16:21
@matanelevi Sync alone is not enough because there are types which are not Send, but are Sync.
and vice verse , there are types which are Sync but are not Send.
You can't say that Cell is Sync, this will lead to data races. But you want to be able to send a Cell from one thread to another. This is allowed because Cell is Send.
Matanel Levi
@matanelevi
Dec 26 2016 17:06
@matklad but can't you say that every type can be Send, but not every type can be Sync?
Aleksey Kladov
@matklad
Dec 26 2016 18:14
@matanelevi No, Rc is not Send. Here is an example which would involve a data race if Rc was Send:
use std::rc::Rc;

fn main() {
    let x = Rc::new(1);
    let y = x.clone();
    foo(y);
    // this bumps ref count on thread 1
    x.clone();
}

fn foo(c: Rc<i32>) {
    ::std::thread::spawn(move || {
        // this bumps ref cont on thread 2.
        // For `Rc`, count increment is not atomic, so
        // this is a race.
        c.clone()
    });
}
This program would compile fine with Arc, which is like Rc, but uses atomic increment for the reference count and is thus thread safe and Send.
It's instrumental to compare this situation with C++. In C++, there's shared_ptr which is like Arc: thread safe, but slower. And there's no analogue of Rc in standard C++, because it would be to dangerous to use: there's no Send in C++, and so type system can not prevent you from sharing a non thread safe objects across threads.
Matanel Levi
@matanelevi
Dec 26 2016 18:24
@matklad ahm... it's still seems vague a bit. thanks :)