Make concurrent examples actually run concurrently

If we end the `scoped` call with a semicolon, the `JoinGuard` will be
dropped and not returned from the `map`. The thread will start up and
we immediately block, making for a very expensive sequential loop.
This commit is contained in:
Jake Goulding 2015-03-08 16:10:19 -04:00
parent a6ebd26208
commit b426a242e3

View file

@ -430,7 +430,7 @@ fn main() {
thread::scoped(move || { thread::scoped(move || {
numbers[i] += 1; numbers[i] += 1;
println!("numbers[{}] is {}", i, numbers[i]); println!("numbers[{}] is {}", i, numbers[i]);
}); })
}).collect(); }).collect();
} }
``` ```
@ -442,7 +442,7 @@ It gives us this error:
7 thread::scoped(move || { 7 thread::scoped(move || {
8 numbers[i] += 1; 8 numbers[i] += 1;
9 println!("numbers[{}] is {}", i, numbers[i]); 9 println!("numbers[{}] is {}", i, numbers[i]);
10 }); 10 })
error: aborting due to previous error error: aborting due to previous error
``` ```
@ -483,7 +483,7 @@ fn main() {
let mut array = number.lock().unwrap(); let mut array = number.lock().unwrap();
array[i] += 1; array[i] += 1;
println!("numbers[{}] is {}", i, array[i]); println!("numbers[{}] is {}", i, array[i]);
}); })
}).collect(); }).collect();
} }
``` ```
@ -543,7 +543,7 @@ fn main() {
let guards: Vec<_> = (0..3).map(|i| { let guards: Vec<_> = (0..3).map(|i| {
thread::scoped(move || { thread::scoped(move || {
println!("{}", numbers[i]); println!("{}", numbers[i]);
}); })
}).collect(); }).collect();
} }
``` ```