Skip to content
Quiz · Threads · lesson 7 of 7

Quiz — Arc<Mutex> and Rc

Question 1 of 2

What does the program print?

Rust
use std::sync::{Arc, Mutex};

fn main() {
    let counter = Arc::new(Mutex::new(0));
    let mut handles = Vec::new();
    for _ in 0..4 {
        let c = Arc::clone(&counter);
        handles.push(std::thread::spawn(move || *c.lock().unwrap() += 1));
    }
    handles.into_iter().for_each(|h| h.join().unwrap());
    println!("{}", counter.lock().unwrap());
}