Search code examples
rusthyper

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


This is a continuation of How to re-use a value from the outer scope inside a closure in Rust? , opened new Q for better presentation.

// 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")
            }
        }))
    });

Unfortunately, I get RefCell<std::string::String> cannot be shared between threads safely:

enter image description here


Solution

  • RefCell only works on single threads. You will need to use Mutex which is similar but works on multiple threads. You can read more about Mutex here: https://doc.rust-lang.org/std/sync/struct.Mutex.html.

    Here is an example of moving an Arc<Mutex<>> into a closure:

    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);
        };
    }