Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Threads with Arc<RefCell<T>> #21469

Closed
Munksgaard opened this issue Jan 21, 2015 · 5 comments · Fixed by #21472
Closed

Threads with Arc<RefCell<T>> #21469

Munksgaard opened this issue Jan 21, 2015 · 5 comments · Fixed by #21472

Comments

@Munksgaard
Copy link
Contributor

I tried doing the following

use std::thread::Thread;
use std::sync::Arc;
use std::cell::RefCell;


fn main() {
    let r = Arc::new(RefCell::new(42u8));
    let r1 = r.clone();
    let t = Thread::scoped(move || {
        loop {
            match r1.try_borrow_mut() {
                Some(n) => { *n += 1 ; break }
                _ => { }
            }
        }
    });

    t.join();
    println!("{}", *r.borrow_mut());
}

Rust complains:

$ rustc foo.rs
foo.rs:9:13: 9:27 error: the trait `core::marker::Sync` is not implemented for the type `core::cell::UnsafeCell<u8>`
foo.rs:9     let t = Thread::scoped(move || {
                     ^~~~~~~~~~~~~~
foo.rs:9:13: 9:27 error: the trait `core::marker::Sync` is not implemented for the type `core::cell::UnsafeCell<usize
>`                                                                                                                  
foo.rs:9     let t = Thread::scoped(move || {
                     ^~~~~~~~~~~~~~
error: aborting due to 2 previous errors

(Note that my code doesn't involve any UnsafeCells at all)

I was under the impression that, to get a shared mutable state, you wrapped a RefCell in an Arc. The docs even mention that "It's very common then to put a RefCell inside shared pointer types to reintroduce mutability", right after mentioning Arc as a smart shared pointer type. However, @eddyb mentioned on IRC that that might not be the case and suggested that it might be a documentation issue.

$ rustc --version
rustc 1.0.0-dev (3bf41dafc 2015-01-20 06:45:02 +0000)
@Thiez
Copy link
Contributor

Thiez commented Jan 21, 2015

RefCell is for single-threaded scenarios. Have you considered using std::sync::Mutex?

The error message from the compiler could be better.

@Munksgaard
Copy link
Contributor Author

Oh yes, I figured it out, but the docs should probably be updated :)

Edit: And yes, the error message is misleading.

@gobwas
Copy link

gobwas commented Jul 9, 2015

@Thiez Does RwLock is for single threaded too?

@eddyb
Copy link
Member

eddyb commented Jul 9, 2015

@gobwas no, RwLock and Mutex are multi-threaded locks.

@gobwas
Copy link

gobwas commented Jul 9, 2015

@eddyb 🍻 thanks )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants