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:
parent
a6ebd26208
commit
b426a242e3
1 changed files with 4 additions and 4 deletions
|
@ -430,7 +430,7 @@ fn main() {
|
|||
thread::scoped(move || {
|
||||
numbers[i] += 1;
|
||||
println!("numbers[{}] is {}", i, numbers[i]);
|
||||
});
|
||||
})
|
||||
}).collect();
|
||||
}
|
||||
```
|
||||
|
@ -442,7 +442,7 @@ It gives us this error:
|
|||
7 thread::scoped(move || {
|
||||
8 numbers[i] += 1;
|
||||
9 println!("numbers[{}] is {}", i, numbers[i]);
|
||||
10 });
|
||||
10 })
|
||||
error: aborting due to previous error
|
||||
```
|
||||
|
||||
|
@ -483,7 +483,7 @@ fn main() {
|
|||
let mut array = number.lock().unwrap();
|
||||
array[i] += 1;
|
||||
println!("numbers[{}] is {}", i, array[i]);
|
||||
});
|
||||
})
|
||||
}).collect();
|
||||
}
|
||||
```
|
||||
|
@ -543,7 +543,7 @@ fn main() {
|
|||
let guards: Vec<_> = (0..3).map(|i| {
|
||||
thread::scoped(move || {
|
||||
println!("{}", numbers[i]);
|
||||
});
|
||||
})
|
||||
}).collect();
|
||||
}
|
||||
```
|
||||
|
|
Loading…
Reference in a new issue