I have made this code:
#![allow(unused_must_use)]
use std::sync::{Arc, Mutex};
use std::thread;
fn main() {
let v = Arc::new(Mutex::new(vec![1, 2]));
let u = v.clone();
let th = thread::spawn(move || {
let mut vector = u.lock().unwrap();
vector.push(4);
});
let mut vector = v.lock().unwrap();
vector.push(5);
th.join();
println!("{}, {:?}", vector.len(), *vector );
}
And it compiles fine, but when I try to run the program, I get a "timeout triggered!" message.
Just to recapitulate, Rust aims to avoid thread-unsafety, deadlocks and so on. Almost all the cases that a code seems to break the Rust's rules, it doesn't compile. Then, I thought that avoiding deadlocks and thread-safety were rules and it doesn't compile when I break them.
In this case, it seems to be a deadlock, because of let mut vector = v.lock().unwrap();
before the th.join()
. So, my question is: what is this supposed to be? A thing that the compiler still doesn't capture and interpret it as a bug, or is this intentional?