How to correctly exit the thread blocking on mpsc::Receiver

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) => /* Do Something, inform main thread etc */,
                        Err(_) => break,
                    }
                }
            })),
        }
    }
}

impl Drop for B {
    fn drop(&mut self) {
        // Want to do something like "sender.close()/receiver.close()" etc so that the following
        // thread joins. But there is no such function. How do i break the following thread ?
        self.join_handle().take().unwrap().join().unwrap();
    }
}

Is there a way to cleanly exit under such a circumstance ? The thing is that when either receiver or sender is dropped the other sniffs this and gives an error. In case of receiver it will be woken up and will yeild an error in which case i am breaking out of the infinite and blocking loop above. However how do i do that explicitly using this very property of channels, without resorting to other flags etc., and cleanly exit my thread deterministically?

If you drop all senders, then the channel should close. In your impl Drop for B, can you just drop(the_sender)?

since the sender is with A you mean i should do an explicit drop(A) in impl Drop for B you mean?
Something like self.objA.drop() ?

Yes. Like drop(self.objA). (You might need to do a trick that let you acquire ownership of objA in a mutable borrow of B. e.g., store it as an Option<A> and use drop(self.objA.take().unwrap() or use std::mem::replace.)

stackoverflow cross post: rust - How to correctly exit the thread blocking on mpsc::Receiver - Stack Overflow