Skip to content

Remove old_io from trpl/concurrency.md #24024

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

Closed
wants to merge 5 commits into from
Closed
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
32 changes: 10 additions & 22 deletions src/doc/trpl/concurrency.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,14 @@ When `guard` goes out of scope, it will block execution until the thread is
finished. If we didn't want this behaviour, we could use `thread::spawn()`:

```
# #![feature(old_io, std_misc)]
use std::thread;
use std::old_io::timer;
use std::time::Duration;

fn main() {
thread::spawn(|| {
println!("Hello from a thread!");
});

timer::sleep(Duration::milliseconds(50));
thread::sleep_ms(50);
}
```

Expand Down Expand Up @@ -147,10 +144,7 @@ As an example, here is a Rust program that would have a data race in many
languages. It will not compile:

```ignore
# #![feature(old_io, std_misc)]
use std::thread;
use std::old_io::timer;
use std::time::Duration;

fn main() {
let mut data = vec![1u32, 2, 3];
Expand All @@ -161,14 +155,14 @@ fn main() {
});
}

timer::sleep(Duration::milliseconds(50));
thread::sleep_ms(50);
}
```

This gives us an error:

```text
12:17 error: capture of moved value: `data`
8:17 error: capture of moved value: `data`
data[i] += 1;
^~~~
```
Expand All @@ -187,10 +181,7 @@ only one person at a time can mutate what's inside. For that, we can use the
but for a different reason:

```ignore
# #![feature(old_io, std_misc)]
use std::thread;
use std::old_io::timer;
use std::time::Duration;
use std::sync::Mutex;

fn main() {
Expand All @@ -203,17 +194,17 @@ fn main() {
});
}

timer::sleep(Duration::milliseconds(50));
thread::sleep_ms(50);
}
```

Here's the error:

```text
<anon>:11:9: 11:22 error: the trait `core::marker::Send` is not implemented for the type `std::sync::mutex::MutexGuard<'_, collections::vec::Vec<u32>>` [E0277]
<anon>:9:9: 9:22 error: the trait `core::marker::Send` is not implemented for the type `std::sync::mutex::MutexGuard<'_, collections::vec::Vec<u32>>` [E0277]
<anon>:11 thread::spawn(move || {
^~~~~~~~~~~~~
<anon>:11:9: 11:22 note: `std::sync::mutex::MutexGuard<'_, collections::vec::Vec<u32>>` cannot be sent between threads safely
<anon>:9:9: 9:22 note: `std::sync::mutex::MutexGuard<'_, collections::vec::Vec<u32>>` cannot be sent between threads safely
<anon>:11 thread::spawn(move || {
^~~~~~~~~~~~~
```
Expand All @@ -232,11 +223,8 @@ guard across thread boundaries, which gives us our error.
We can use `Arc<T>` to fix this. Here's the working version:

```
# #![feature(old_io, std_misc)]
use std::sync::{Arc, Mutex};
use std::thread;
use std::old_io::timer;
use std::time::Duration;

fn main() {
let data = Arc::new(Mutex::new(vec![1u32, 2, 3]));
Expand All @@ -249,7 +237,7 @@ fn main() {
});
}

timer::sleep(Duration::milliseconds(50));
thread::sleep_ms(50);
}
```

Expand All @@ -258,11 +246,9 @@ handle is then moved into the new thread. Let's examine the body of the
thread more closely:

```
# #![feature(old_io, std_misc)]
# use std::sync::{Arc, Mutex};
# use std::thread;
# use std::old_io::timer;
# use std::time::Duration;
#
# fn main() {
# let data = Arc::new(Mutex::new(vec![1u32, 2, 3]));
# for i in 0..2 {
Expand All @@ -272,6 +258,8 @@ thread::spawn(move || {
data[i] += 1;
});
# }
#
# thread::sleep_ms(50);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why add this sleep_ms

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That block is supposed to be a mirror of the block above it, but with most lines commented out to focus on the inner function body.

For whatever reason, it didn't include the sleep, even in the current version of the document (https://github.com/rust-lang/rust/blob/80def6c2447d23a624e611417f24cf0ab2a5a676/src/doc/trpl/concurrency.md)

This is effectively a no-op, since the commented parts get elided when rendered (see http://doc.rust-lang.org/book/concurrency.html), but it was clearly intended to be a mirror of the previous block at some point. This brings it back into line with that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice catch

# }
```

Expand Down