FAQ - Help and questions

Why does the compiler say my type cannot be sent between threads when I start a worker?

#threads#send#rc#guipublished 0 replies0 views
ReplyWatch
Share
Hacker NewsRedditLinkedInE-mail

Short answer

The error means the closure captures a value whose type does not implement Send, such as Rc, a raw pointer or a toolkit handle tied to the UI thread. Send plain data to the worker instead, or use Arc for shared data, and send results back through a channel. The real error message is shown below.

Checked with rustc 1.93.0, Windows 11; the failing program was compiled and the message below is its real output (shortened).

Frequently asked question, written and answered by the Ruststudio team. Replies are open: corrections are welcome.

#1

Answer#

thread::spawn requires the closure, and everything it captures, to be Send: safe to move to another thread. Some types are deliberately not Send. The compiler stops you at build time, which is the point: it prevents a data race.

The usual cause in a desktop program is Rc. It counts references without atomic operations, so two threads touching the count could corrupt it.

src/main.rsrust
use std::rc::Rc;
use std::thread;

fn main() {
    let label = Rc::new(String::from("status"));
    let handle = thread::spawn(move || {
        println!("{label}");
    });
    handle.join().unwrap();
}
text
error[E0277]: `Rc<String>` cannot be sent between threads safely
 --> src/main.rs:6:32
  |
6 |       let handle = thread::spawn(move || {
  |                    ------------- ^------
  |                    |             |
  |  __________________|_____________within this `{closure@src/main.rs:6:32: 6:39}`
  | |                  |
  | |                  required by a bound introduced by this call
7 | |         println!("{label}");
8 | |     });
  | |_____^ `Rc<String>` cannot be sent between threads safely
  |
  = help: within `{closure@src/main.rs:6:32: 6:39}`, the trait `Send` is not implemented for `Rc<String>`

(File paths in the message were shortened by hand; the rest is as printed.)

Ways to fix it, depending on what you need:

  1. The worker only needs a copy of the data. Clone the plain value (String, numbers, a Vec) and move the clone in.
  2. Both sides need the same data. Replace Rc with Arc, and add a Mutex (or RwLock) if either side changes it.
  3. The worker needs to change the UI. It cannot touch UI objects. Send a message through a channel and let the UI thread apply the change. Toolkit handles are often not Send for the same reason; the toolkit's documentation says how to reach the UI from another thread (for example Slint's weak handle with invoke_from_event_loop, or egui's Context::request_repaint).

Common mistakes#

  • Wrapping the type in unsafe impl Send to make the error go away. That hides the data race instead of removing it.
  • Moving a whole App struct into the thread. Move the few fields the worker needs.

What does Sync mean?#

A type is Sync if a shared reference to it can be used from several threads. Arc<Mutex<T>> is Send and Sync when T is Send.

Sources#

    QuoteReply
    Share
    Hacker NewsRedditLinkedInE-mail

    No replies yet