`RefCell<std::string::String>` 不能在线程间安全共享?

`RefCell<std::string::String>` cannot be shared between threads safely?

这是的延续,为了更好的呈现,打开了新Q。

// main.rs

    // The value will be modified eventually inside `main` 
    // and a http request should respond with whatever "current" value it holds.
    let mut test_for_closure :Arc<RefCell<String>> = Arc::new(RefCell::from("Foo".to_string()));

// ...

    // Handler for HTTP requests
    // From https://docs.rs/hyper/0.14.8/hyper/service/fn.service_fn.html
    let make_svc = make_service_fn(|_conn| async {
        Ok::<_, Infallible>(service_fn(|req: Request<Body>| async move {
            if req.version() == Version::HTTP_11 {
                let foo:String = *test_for_closure.borrow();
                Ok(Response::new(Body::from(foo.as_str())))
            } else {
                Err("not HTTP/1.1, abort connection")
            }
        }))
    });

不幸的是,我得到 RefCell<std::string::String> cannot be shared between threads safely:

您的错误消息中的第一个错误是 Sync 未针对 RefCell<String> 实现。这是设计使然,如 Syncrustdoc:

所述

Types that are not Sync are those that have “interior mutability” in a non-thread-safe form, such as Cell and RefCell. These types allow for mutation of their contents even through an immutable, shared reference. For example the set method on Cell takes &self, so it requires only a shared reference &Cell. The method performs no synchronization, thus Cell cannot be Sync.

因此在线程之间共享 RefCell 是不安全的,因为您可能会通过常规的共享引用引起数据竞争。

但是如果你把它包裹在 Arc 中呢?好吧,rustdoc又很清楚了:

Arc will implement Send and Sync as long as the T implements Send and Sync. Why can’t you put a non-thread-safe type T in an Arc to make it thread-safe? This may be a bit counter-intuitive at first: after all, isn’t the point of Arc thread safety? The key is this: Arc makes it thread safe to have multiple ownership of the same data, but it doesn’t add thread safety to its data. Consider Arc<RefCell>. RefCell isn’t Sync, and if Arc was always Send, Arc<RefCell> would be as well. But then we’d have a problem: RefCell is not thread safe; it keeps track of the borrowing count using non-atomic operations.

In the end, this means that you may need to pair Arc with some sort of std::sync type, usually Mutex.

Arc<T> 不会是 Sync 除非 TSync 因为同样的原因。鉴于此,您可能应该使用 std/tokio Mutex 而不是 RefCell

RefCell 仅适用于单线程。您将需要使用类似但适用于多个线程的 Mutex。您可以在此处阅读有关 Mutex 的更多信息:https://doc.rust-lang.org/std/sync/struct.Mutex.html.

下面是将 Arc> 移动到闭包中的示例:

use std::sync::{Arc, Mutex};

fn main() {
    let mut test: Arc<Mutex<String>> = Arc::new(Mutex::from("Foo".to_string()));

    let mut test_for_closure = Arc::clone(&test);
    let closure = || async move {
        // lock it so it cant be used in other threads
        let foo = test_for_closure.lock().unwrap();
        println!("{}", foo);
    };
}