Sharing mutable self between multiple threads

前端 未结 1 586
情书的邮戳
情书的邮戳 2021-01-23 02:16

I have a server that accepts connections from multiple clients. Each client could send a message to the server, which is broadcast to all other clients. The problem is that the

相关标签:
1条回答
  • 2021-01-23 02:34

    The error is pretty much unrelated to having multiple threads. The issue is, as the compiler says, that selfm is already borrowed in the line

    for stream in selfm.listener.incoming() {
    

    so it cannot be mutably borrowed in the line

    selfm.clients.push(stream);
    

    One way to fix this is to destructure selfm before the loop, so the borrows don't conflict. Your start method will then look as follows:

    fn start(mut self) {
        let mut handles = vec![];
        let a : Arc<Mutex<Server>> = Arc::new(Mutex::new(self));
        let mut selfm = a.lock().unwrap();
    
         // destructure selfm here to get a reference to the listener and a mutable reference to the clients
        let Server { ref listener, ref mut clients} = *selfm;
        for stream in listener.incoming() { // listener can be used here
            match stream {
                Ok(stream) => {
                    clients.push(stream); // clients can be mutated here
                    let aa = a.clone();
                    handles.push(thread::spawn(move || {
                        aa.lock().unwrap().handle();
                    }));
                },
                Err(e) => { println!("{}", e); },
            }
        }
    }
    

    (That being said, you're right to be concerned about the locking, since the mutex will remain locked until selfm goes out of scope, i.e. only when start terminates, i.e. never. I would suggest an alternative design, but it's not really clear to me why you want the threads to have access to the server struct.)

    0 讨论(0)
提交回复
热议问题