Lock() stuck and I don't know how should I change my design

here is my code. the structurs should be event base and also multithreaded. any design idea ?

use std::sync::{Arc,Mutex};
trait dd{
    fn notify(&mut self);
}
struct a {
    write: Vec<Arc<Mutex<dyn dd>>>// spin::Mutex
}
impl dd for a{
    fn notify(&mut self){
        for notif in write{
            (*self.notif.lock()).notify();// spin::Mutex
        }
    }
}
struct conn{
    should_notify: Vec<Arc<Mutex<dyn dd>>>// spin::Mutex
}
impl dd for a{
    fn notify(&mut self){
        for notif in should_notify{
            (*self.notif.lock()).notify();// spin::Mutex
        }
    }
}
fn main() {
    let mut a  = a{write: Vec::new()};
    let mut conn = Arc::from(Mutex::from(conn{should_notify: Vec::new()}));
    a.write.push(conn.clone());
    std::thread::spawn(||{
        loop{
            (*conn.lock()).notify();
            std::thread::sleep(std::time::Duration::frim_millis(10));
            
        }
    });
}

rust playground

Playground does not compile, is that intentional?

I used spin library.so that mutex behave a little different. That's the reason this syntax doesn't compile.

There is spin in playground, but by replacing std::sync::Mutex with spin::Mutex this doesn't compile either.

1 Like

here is fixed code.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.