这是我正在尝试编写的简化代码:
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
}
但是,RwLockReadGuard
is notSend
,因此此代码无法编译。
我应该如何解决RwLockReadGuard
跨线程边界发送的问题?