|
|
|
|
|
by brink
476 days ago
|
|
use std::{
sync::{
Arc,
atomic::{AtomicBool, AtomicUsize, Ordering},
},
time::Duration,
}; fn main() {
let num = Arc::new(AtomicUsize::new(0));
let finished = Arc::new(AtomicBool::new(false)); for _ in 0..10 {
std::thread::spawn({
let num = num.clone();
let finished = finished.clone();
move || {
while !finished.load(Ordering::SeqCst) {
num.fetch_add(1, Ordering::SeqCst);
}
}
});
}
std::thread::sleep(Duration::from_millis(1000));
finished.store(true, Ordering::SeqCst);
}
|
|