|
5 | 5 | // set of numbers within 8 different threads simultaneously. Each thread is |
6 | 6 | // going to get the sum of every eighth value, with an offset. |
7 | 7 | // |
| 8 | +// 本练习中,我们有一个名为 "numbers" 的 u32 向量,包含从 0 到 99 的值 -- [ 0, 1, 2, ..., 98, 99 ]。 |
| 9 | +// 我们希望在 8 个不同的线程中同时使用这组数字。每个线程将计算每隔 8 个数的和,带有一个偏移量。 |
| 10 | +// |
8 | 11 | // The first thread (offset 0), will sum 0, 8, 16, ... |
| 12 | +// 第一个线程(偏移量 0),会累加 0, 8, 16, ... |
9 | 13 | // The second thread (offset 1), will sum 1, 9, 17, ... |
10 | 14 | // The third thread (offset 2), will sum 2, 10, 18, ... |
11 | 15 | // ... |
|
21 | 25 | // |
22 | 26 | // Execute `rustlings hint arc1` or use the `hint` watch subcommand for a hint. |
23 | 27 |
|
24 | | -// I AM NOT DONE |
25 | 28 |
|
26 | 29 | #![forbid(unused_imports)] // Do not change this, (or the next) line. |
27 | 30 | use std::sync::Arc; |
28 | 31 | use std::thread; |
29 | 32 |
|
30 | 33 | fn main() { |
31 | 34 | let numbers: Vec<_> = (0..100u32).collect(); |
32 | | - let shared_numbers = // TODO |
| 35 | + let shared_numbers = Arc::new(numbers);// TODO |
33 | 36 | let mut joinhandles = Vec::new(); |
34 | 37 |
|
35 | 38 | for offset in 0..8 { |
36 | | - let child_numbers = // TODO |
| 39 | + let child_numbers = shared_numbers.clone(); // TODO |
37 | 40 | joinhandles.push(thread::spawn(move || { |
38 | 41 | let sum: u32 = child_numbers.iter().filter(|&&n| n % 8 == offset).sum(); |
39 | 42 | println!("Sum of offset {} is {}", offset, sum); |
|
0 commit comments