Medium
Sharing an Arc<Cell<u32>> counter across two threads
What happens when this program is compiled?
main.rs
use std::cell::Cell;
use std::sync::Arc;
use std::thread;
fn main() {
let counter = Arc::new(Cell::new(0u32));
let mut handles = Vec::new();
for _ in 0..2 {
let counter = Arc::clone(&counter);
handles.push(thread::spawn(move || {
counter.set(counter.get() + 1);
}));
}
for h in handles {
h.join().unwrap();
}
println!("{}", counter.get());
}31 of 53 in Rust