impl A { fn new() -> (A, std::sync::mpsc::Receiver<Data>) { let (sender, receiver) = std::sync::mpsc::channel(); let objA = A { sender: sender, }; // A spawns threads, clones and uses sender etc (objA, receiver) } } impl B { fn new() -> B { let (objA, receiver) = A::new(); B { a: objA, join_handle: Some(std::thread::spwan(move || { loop { match receiver.recv() { Ok(data) => , Err(_) => break, } } })), } } } impl Drop for B { fn drop(&mut self) {
Is there a way to fail under such circumstances? The fact is that when the receiver or sender falls, the other sniffs it and gives an error. In the case of receiver it will be woken up and give an error, in which case I will break out of the infinite blocking cycle above. However, how can I explicitly use this property of channels without resorting to other flags in combination with try_recv() , etc., and cleanly exit my stream deterministically?
source share