Mercurial > hg > Members > kono > rust-tutorial
view src/t16thread/src/racecondition.rs @ 8:2c6285996268
pub fn worked
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 12 Jan 2021 14:56:07 +0900 |
parents | 8768d36c3b69 |
children | aaba40049c28 |
line wrap: on
line source
// #![deny(missing_docs)] use std::sync::{Arc, Mutex}; use std::thread; pub fn mainr() { let counter = Arc::new(Mutex::new(0)); let mut handles = vec![]; for _ in 0..10 { let counter = Arc::clone(&counter); let handle = thread::spawn(move || { let mut num = counter.lock().unwrap(); *num += 1; }); handles.push(handle); } for handle in handles { handle.join().unwrap(); } println!("Result: {}", *counter.lock().unwrap()); }