Async/await: does Rust ensure thread safety when using multithreaded executor?

From https://rust-lang.github.io/async-book/03_async_await/01_chapter.html:

Note that, when using a multithreaded Future executor, a Future may move between threads, so any variables used in async bodies must be able to travel between threads, as any .await can potentially result in a switch to a new thread.
This means that it is not safe to use Rc , &RefCell or any other types that don't implement the Send trait, including references to types that don't implement the Sync trait.

Does Rust actually compiles when Rc is referenced in an async body? I.e., should I manually think about this situation, or will Rust make sure such situation always cause a compile error?

If an Rc exists across an .await, the type of that async block/function's future will not be Send, and the compiler will give an error when you give it to spawn since it requires the passed type to be Send.

1 Like

Thank you!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.