-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconcurrency.rs
44 lines (34 loc) · 886 Bytes
/
concurrency.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use std::thread;
fn main() {
let mut acc = Box::new(0);
let threads: Vec<_> = (0..10).map(|_| {
thread::scoped(move || {
for _ in 0..100000 {
*acc += 1;
}
})
}).collect();
for t in threads {
t.join();
}
println!("{}", acc);
}
// Following is the correct, valid code
// use std::thread;
// use std::sync::{Arc,Mutex};
// fn main() {
// let mut acc = Arc::new(Mutex::new(Box::new(0)));
// let threads: Vec<_> = (0..10).map(|_| {
// let acc = acc.clone();
// thread::scoped(move || {
// let mut acc = acc.lock().unwrap();
// for _ in 0..100000 {
// **acc = **acc + 1;
// }
// })
// }).collect();
// for t in threads {
// t.join();
// }
// println!("{}", **acc.lock().unwrap());
// }