Skip to content

Introduction concurrent examples aren't actually concurrent #23202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 9, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions src/doc/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -389,11 +389,11 @@ safe concurrent programs.
Here's an example of a concurrent Rust program:

```{rust}
use std::thread::Thread;
use std::thread;

fn main() {
let guards: Vec<_> = (0..10).map(|_| {
Thread::scoped(|| {
thread::scoped(|| {
println!("Hello, world!");
})
}).collect();
Expand Down Expand Up @@ -421,16 +421,16 @@ problem.
Let's see an example. This Rust code will not compile:

```{rust,ignore}
use std::thread::Thread;
use std::thread;

fn main() {
let mut numbers = vec![1, 2, 3];

let guards: Vec<_> = (0..3).map(|i| {
Thread::scoped(move || {
thread::scoped(move || {
numbers[i] += 1;
println!("numbers[{}] is {}", i, numbers[i]);
});
})
}).collect();
}
```
Expand All @@ -439,10 +439,10 @@ It gives us this error:

```text
7:25: 10:6 error: cannot move out of captured outer variable in an `FnMut` closure
7 Thread::scoped(move || {
7 thread::scoped(move || {
8 numbers[i] += 1;
9 println!("numbers[{}] is {}", i, numbers[i]);
10 });
10 })
error: aborting due to previous error
```

Expand Down Expand Up @@ -471,19 +471,19 @@ mutation doesn't cause a data race.
Here's what using an Arc with a Mutex looks like:

```{rust}
use std::thread::Thread;
use std::thread;
use std::sync::{Arc,Mutex};

fn main() {
let numbers = Arc::new(Mutex::new(vec![1, 2, 3]));

let guards: Vec<_> = (0..3).map(|i| {
let number = numbers.clone();
Thread::scoped(move || {
thread::scoped(move || {
let mut array = number.lock().unwrap();
array[i] += 1;
println!("numbers[{}] is {}", i, array[i]);
});
})
}).collect();
}
```
Expand Down Expand Up @@ -535,15 +535,15 @@ As an example, Rust's ownership system is _entirely_ at compile time. The
safety check that makes this an error about moved values:

```{rust,ignore}
use std::thread::Thread;
use std::thread;

fn main() {
let numbers = vec![1, 2, 3];

let guards: Vec<_> = (0..3).map(|i| {
Thread::scoped(move || {
thread::scoped(move || {
println!("{}", numbers[i]);
});
})
}).collect();
}
```
Expand Down