Skip to content

Commit 894cd39

Browse files
committed
Rollup merge of #23202 - shepmaster:intro-threads-not-concurrent, r=steveklabnik
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.
2 parents 094b6bc + b426a24 commit 894cd39

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

src/doc/intro.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -389,11 +389,11 @@ safe concurrent programs.
389389
Here's an example of a concurrent Rust program:
390390
391391
```{rust}
392-
use std::thread::Thread;
392+
use std::thread;
393393
394394
fn main() {
395395
let guards: Vec<_> = (0..10).map(|_| {
396-
Thread::scoped(|| {
396+
thread::scoped(|| {
397397
println!("Hello, world!");
398398
})
399399
}).collect();
@@ -421,16 +421,16 @@ problem.
421421
Let's see an example. This Rust code will not compile:
422422
423423
```{rust,ignore}
424-
use std::thread::Thread;
424+
use std::thread;
425425
426426
fn main() {
427427
let mut numbers = vec![1, 2, 3];
428428
429429
let guards: Vec<_> = (0..3).map(|i| {
430-
Thread::scoped(move || {
430+
thread::scoped(move || {
431431
numbers[i] += 1;
432432
println!("numbers[{}] is {}", i, numbers[i]);
433-
});
433+
})
434434
}).collect();
435435
}
436436
```
@@ -439,10 +439,10 @@ It gives us this error:
439439
440440
```text
441441
7:25: 10:6 error: cannot move out of captured outer variable in an `FnMut` closure
442-
7 Thread::scoped(move || {
442+
7 thread::scoped(move || {
443443
8 numbers[i] += 1;
444444
9 println!("numbers[{}] is {}", i, numbers[i]);
445-
10 });
445+
10 })
446446
error: aborting due to previous error
447447
```
448448
@@ -471,19 +471,19 @@ mutation doesn't cause a data race.
471471
Here's what using an Arc with a Mutex looks like:
472472
473473
```{rust}
474-
use std::thread::Thread;
474+
use std::thread;
475475
use std::sync::{Arc,Mutex};
476476
477477
fn main() {
478478
let numbers = Arc::new(Mutex::new(vec![1, 2, 3]));
479479
480480
let guards: Vec<_> = (0..3).map(|i| {
481481
let number = numbers.clone();
482-
Thread::scoped(move || {
482+
thread::scoped(move || {
483483
let mut array = number.lock().unwrap();
484484
array[i] += 1;
485485
println!("numbers[{}] is {}", i, array[i]);
486-
});
486+
})
487487
}).collect();
488488
}
489489
```
@@ -535,15 +535,15 @@ As an example, Rust's ownership system is _entirely_ at compile time. The
535535
safety check that makes this an error about moved values:
536536
537537
```{rust,ignore}
538-
use std::thread::Thread;
538+
use std::thread;
539539
540540
fn main() {
541541
let numbers = vec![1, 2, 3];
542542
543543
let guards: Vec<_> = (0..3).map(|i| {
544-
Thread::scoped(move || {
544+
thread::scoped(move || {
545545
println!("{}", numbers[i]);
546-
});
546+
})
547547
}).collect();
548548
}
549549
```

0 commit comments

Comments
 (0)