1

这是我正在尝试编写的简化代码:

use parking_lot::RwLock;

let buffers: Box<[RwLock<MyBuffer>]> = Box::new([ ... ]);
let (sender, receiver) = std::sync::mpsc::channel();

thread::spawn(move || {
    let guard = buffers[ ... ].write().unwrap();
    expensive_computation(&mut *guard);
    sender.send(guard.downgrade()).unwrap();
});

for message in receiver.iter() {
    // Handle the message
}

但是,RwLockReadGuardis notSend,因此此代码无法编译。

我应该如何解决RwLockReadGuard跨线程边界发送的问题?

4

0 回答 0