I'm doing a simple program to simulate a supermarket queue to learn Rust. The program consists in two threads. One is adding people (char 'x') to the supermarket queue and the other one is removing them.
I use Arc and Mutex to handle the concurrency but it seems that the first thread never free the var, so the second doesn't work.
The code is the follows:
let queue = Arc::new(Mutex::new(Vec::<char>::new()));
let queue1 = Arc::clone(&queue);
thread::spawn(move || loop {
let mut aux = queue1.lock().unwrap();
aux.push('x');
print_queue(&aux);
thread::sleep(Duration::from_secs(3));
});
thread::spawn(move || loop {
let mut aux = queue.lock().unwrap();
println!("AUX state: {:?}", aux);
if !&aux.is_empty() {
aux.pop();
}
print_queue(&aux);
let mut rng = rand::thread_rng();
thread::sleep(Duration::from_secs(rng.gen_range(1..10)));
});
The print of the AUX state never shows. What I'm doing wrong?