mirror of
https://github.com/rust-lang/rustlings.git
synced 2024-12-26 00:00:03 +03:00
arc1 solution
This commit is contained in:
parent
f3842aa746
commit
a943f5ba32
|
@ -1,39 +1,42 @@
|
||||||
// In this exercise, we are given a Vec of u32 called "numbers" with values
|
// In this exercise, we are given a `Vec` of u32 called `numbers` with values
|
||||||
// ranging from 0 to 99 -- [ 0, 1, 2, ..., 98, 99 ] We would like to use this
|
// ranging from 0 to 99. We would like to use this set of numbers within 8
|
||||||
// set of numbers within 8 different threads simultaneously. Each thread is
|
// different threads simultaneously. Each thread is going to get the sum of
|
||||||
// going to get the sum of every eighth value, with an offset.
|
// every eighth value with an offset.
|
||||||
//
|
//
|
||||||
// The first thread (offset 0), will sum 0, 8, 16, ...
|
// The first thread (offset 0), will sum 0, 8, 16, …
|
||||||
// The second thread (offset 1), will sum 1, 9, 17, ...
|
// The second thread (offset 1), will sum 1, 9, 17, …
|
||||||
// The third thread (offset 2), will sum 2, 10, 18, ...
|
// The third thread (offset 2), will sum 2, 10, 18, …
|
||||||
// ...
|
// …
|
||||||
// The eighth thread (offset 7), will sum 7, 15, 23, ...
|
// The eighth thread (offset 7), will sum 7, 15, 23, …
|
||||||
//
|
//
|
||||||
// Because we are using threads, our values need to be thread-safe. Therefore,
|
// Because we are using threads, our values need to be thread-safe. Therefore,
|
||||||
// we are using Arc. We need to make a change in each of the two TODOs.
|
// we are using `Arc`.
|
||||||
//
|
|
||||||
// Make this code compile by filling in a value for `shared_numbers` where the
|
|
||||||
// first TODO comment is, and create an initial binding for `child_numbers`
|
|
||||||
// where the second TODO comment is. Try not to create any copies of the
|
|
||||||
// `numbers` Vec!
|
|
||||||
|
|
||||||
#![forbid(unused_imports)] // Do not change this, (or the next) line.
|
// Don't change the lines below.
|
||||||
use std::sync::Arc;
|
#![forbid(unused_imports)]
|
||||||
use std::thread;
|
use std::{sync::Arc, thread};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let numbers: Vec<_> = (0..100u32).collect();
|
let numbers: Vec<_> = (0..100u32).collect();
|
||||||
let shared_numbers = // TODO
|
|
||||||
let mut joinhandles = Vec::new();
|
// TODO: Define `shared_numbers` by using `Arc`.
|
||||||
|
// let shared_numbers = ???;
|
||||||
|
|
||||||
|
let mut join_handles = Vec::new();
|
||||||
|
|
||||||
for offset in 0..8 {
|
for offset in 0..8 {
|
||||||
let child_numbers = // TODO
|
// TODO: Define `child_numbers` using `shared_numbers`.
|
||||||
joinhandles.push(thread::spawn(move || {
|
// let child_numbers = ???;
|
||||||
|
|
||||||
|
let handle = thread::spawn(move || {
|
||||||
let sum: u32 = child_numbers.iter().filter(|&&n| n % 8 == offset).sum();
|
let sum: u32 = child_numbers.iter().filter(|&&n| n % 8 == offset).sum();
|
||||||
println!("Sum of offset {} is {}", offset, sum);
|
println!("Sum of offset {offset} is {sum}");
|
||||||
}));
|
});
|
||||||
|
|
||||||
|
join_handles.push(handle);
|
||||||
}
|
}
|
||||||
for handle in joinhandles.into_iter() {
|
|
||||||
|
for handle in join_handles.into_iter() {
|
||||||
handle.join().unwrap();
|
handle.join().unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1004,7 +1004,7 @@ name = "arc1"
|
||||||
dir = "19_smart_pointers"
|
dir = "19_smart_pointers"
|
||||||
test = false
|
test = false
|
||||||
hint = """
|
hint = """
|
||||||
Make `shared_numbers` be an `Arc` from the numbers vector. Then, in order
|
Make `shared_numbers` be an `Arc` from the `numbers` vector. Then, in order
|
||||||
to avoid creating a copy of `numbers`, you'll need to create `child_numbers`
|
to avoid creating a copy of `numbers`, you'll need to create `child_numbers`
|
||||||
inside the loop but still in the main thread.
|
inside the loop but still in the main thread.
|
||||||
|
|
||||||
|
|
|
@ -1 +1,42 @@
|
||||||
// Solutions will be available before the stable release. Thank you for testing the beta version 🥰
|
// In this exercise, we are given a `Vec` of u32 called `numbers` with values
|
||||||
|
// ranging from 0 to 99. We would like to use this set of numbers within 8
|
||||||
|
// different threads simultaneously. Each thread is going to get the sum of
|
||||||
|
// every eighth value with an offset.
|
||||||
|
//
|
||||||
|
// The first thread (offset 0), will sum 0, 8, 16, …
|
||||||
|
// The second thread (offset 1), will sum 1, 9, 17, …
|
||||||
|
// The third thread (offset 2), will sum 2, 10, 18, …
|
||||||
|
// …
|
||||||
|
// The eighth thread (offset 7), will sum 7, 15, 23, …
|
||||||
|
//
|
||||||
|
// Because we are using threads, our values need to be thread-safe. Therefore,
|
||||||
|
// we are using `Arc`.
|
||||||
|
|
||||||
|
// Don't change the lines below.
|
||||||
|
#![forbid(unused_imports)]
|
||||||
|
use std::{sync::Arc, thread};
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let numbers: Vec<_> = (0..100u32).collect();
|
||||||
|
|
||||||
|
let shared_numbers = Arc::new(numbers);
|
||||||
|
// ^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
let mut join_handles = Vec::new();
|
||||||
|
|
||||||
|
for offset in 0..8 {
|
||||||
|
let child_numbers = Arc::clone(&shared_numbers);
|
||||||
|
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
let handle = thread::spawn(move || {
|
||||||
|
let sum: u32 = child_numbers.iter().filter(|&&n| n % 8 == offset).sum();
|
||||||
|
println!("Sum of offset {offset} is {sum}");
|
||||||
|
});
|
||||||
|
|
||||||
|
join_handles.push(handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
for handle in join_handles.into_iter() {
|
||||||
|
handle.join().unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue