Moving Arc<RwLock<T>> to threads leads to errors regarding missing Send trait


I'm having some issues understanding why this is a problem. When I try to move an Arc<RwLock<T> to a thread I get a long error message that the Send trait is not implemented for T and all the members of T as well.

note: required by a bound in `spawn`
625 |     F: Send + 'static,
    |        ^^^^ required by this bound in `spawn`

Here's a snippet of code where this happens:

pub(crate) fn new(swap_chains: Arc<RwLock<SwapChainMap>>) -> Self {
  let (thread_sender, thread_receiver) = std::sync::mpsc::channel::<PresentMessage>();
  let (response_sender, response_receiver) = std::sync::mpsc::channel::<PresentResultMessage>();
  let child = std::thread::spawn(move || loop {  // <-- error here
    // access swap_chains with write/read
    // send/receive messages  

SwapChainMap is typedef for a custom map structure (pub struct HandleMap<H, T>) and I'm using the 1.57.0 nightly compiler.

Shouldn't this just work? I was under the impression that RwLock provides Send and Sync implementations. Am I missing something?

H and T are not necessarily Send, though. Or 'static, for that matter. Rwlock and Arc can't "wrap" something to make it Send. As such you need a Send bound on them as well.

RwLock is Send only if the underlying value is Send (and same for Sync). Arc is Sync if the underlying value is Send.

Ah I see. Thanks for the info!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.