Incorporate Arc exercise from @ConnyOnny!! 🌟
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
// make this code compile and don't create any copies of the "numbers" Vec.
|
||||
// scroll down for hints
|
||||
// Make this code compile by filling in a value for `shared_numbers` where the
|
||||
// TODO comment is and creating an initial binding for `child_numbers`
|
||||
// somewhere. Try not to create any copies of the `numbers` Vec!
|
||||
// Scroll down for hints :)
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::thread;
|
||||
@@ -9,12 +11,12 @@ fn main() {
|
||||
let shared_numbers = // TODO
|
||||
let mut joinhandles = Vec::new();
|
||||
|
||||
for offset in 0..5 {
|
||||
for offset in 0..8 {
|
||||
joinhandles.push(
|
||||
thread::spawn(move || {
|
||||
let mut i = offset;
|
||||
let mut sum = 0;
|
||||
while i<child_numbers.len() {
|
||||
while i < child_numbers.len() {
|
||||
sum += child_numbers[i];
|
||||
i += 5;
|
||||
}
|
||||
@@ -45,8 +47,9 @@ fn main() {
|
||||
|
||||
|
||||
|
||||
// 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`
|
||||
// inside the loop but still in the main thread.
|
||||
|
||||
|
||||
// In line 6 you must create an Arc from the numbers vector.
|
||||
// You must create the child_numbers inside the loop but still in the main thread.
|
||||
// child_numbers is a clone of the Arc of the numbers, not a copy of the numbers.
|
||||
// `child_numbers` should be a clone of the Arc of the numbers instead of a
|
||||
// thread-local copy of the numbers.
|
||||
|
||||
Reference in New Issue
Block a user