Mercurial > hg > Members > kono > rust-tutorial
annotate src/t16thread/src/racecondition.rs @ 7:8768d36c3b69
thread test
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 12 Jan 2021 14:34:05 +0900 |
parents | |
children | 2c6285996268 |
rev | line source |
---|---|
7 | 1 use std::sync::Mutex; |
2 use std::thread; | |
3 pub fn mainr() { | |
4 let counter = Mutex::new(0); | |
5 let mut handles = vec![]; | |
6 for _ in 0..10 { | |
7 let handle = thread::spawn(move || { | |
8 let mut num = counter.lock().unwrap(); | |
9 | |
10 *num += 1; | |
11 }); | |
12 handles.push(handle); | |
13 } | |
14 | |
15 for handle in handles { | |
16 handle.join().unwrap(); | |
17 } | |
18 | |
19 println!("Result: {}", *counter.lock().unwrap()); | |
20 } |