Skip to content

Commit

Permalink
Rollup merge of rust-lang#29622 - steveklabnik:gh29621, r=bstrie
Browse files Browse the repository at this point in the history
Now that thread::sleep is a real thing, let's use it

Fixes rust-lang#29621

r? @bstrie
  • Loading branch information
Manishearth committed Nov 7, 2015
2 parents 50e8707 + 801f83f commit be4cd07
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
12 changes: 8 additions & 4 deletions src/doc/trpl/concurrency.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ languages. It will not compile:

```ignore
use std::thread;
use std::time::Duration;
fn main() {
let mut data = vec![1, 2, 3];
Expand All @@ -131,7 +132,7 @@ fn main() {
});
}
thread::sleep_ms(50);
thread::sleep(Duration::from_millis(50));
}
```

Expand Down Expand Up @@ -165,6 +166,7 @@ indivisible operations which can't have data races.
```ignore
use std::thread;
use std::sync::Arc;
use std::time::Duration;
fn main() {
let mut data = Arc::new(vec![1, 2, 3]);
Expand All @@ -176,7 +178,7 @@ fn main() {
});
}
thread::sleep_ms(50);
thread::sleep(Duration::from_millis(50));
}
```

Expand Down Expand Up @@ -207,6 +209,7 @@ Here's the working version:
```rust
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;

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

thread::sleep_ms(50);
thread::sleep(Duration::from_millis(50));
}
```

Expand All @@ -241,6 +244,7 @@ Let's examine the body of the thread more closely:
```rust
# use std::sync::{Arc, Mutex};
# use std::thread;
# use std::time::Duration;
# fn main() {
# let data = Arc::new(Mutex::new(vec![1, 2, 3]));
# for i in 0..3 {
Expand All @@ -250,7 +254,7 @@ thread::spawn(move || {
data[i] += 1;
});
# }
# thread::sleep_ms(50);
# thread::sleep(Duration::from_millis(50));
# }
```

Expand Down
23 changes: 13 additions & 10 deletions src/doc/trpl/dining-philosophers.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ eat. Here’s the next version:

```rust
use std::thread;
use std::time::Duration;

struct Philosopher {
name: String,
Expand All @@ -279,7 +280,7 @@ impl Philosopher {
fn eat(&self) {
println!("{} is eating.", self.name);

thread::sleep_ms(1000);
thread::sleep(Duration::from_millis(1000));

println!("{} is done eating.", self.name);
}
Expand Down Expand Up @@ -313,13 +314,13 @@ from the standard library, and so we need to `use` it.
fn eat(&self) {
println!("{} is eating.", self.name);
thread::sleep_ms(1000);
thread::sleep(Duration::from_millis(1000));
println!("{} is done eating.", self.name);
}
```

We now print out two messages, with a `sleep_ms()` in the middle. This will
We now print out two messages, with a `sleep` in the middle. This will
simulate the time it takes a philosopher to eat.

If you run this program, you should see each philosopher eat in turn:
Expand All @@ -345,6 +346,7 @@ Here’s the next iteration:

```rust
use std::thread;
use std::time::Duration;

struct Philosopher {
name: String,
Expand All @@ -360,7 +362,7 @@ impl Philosopher {
fn eat(&self) {
println!("{} is eating.", self.name);

thread::sleep_ms(1000);
thread::sleep(Duration::from_millis(1000));

println!("{} is done eating.", self.name);
}
Expand Down Expand Up @@ -493,6 +495,7 @@ Let’s modify the program to use the `Table`:

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

struct Philosopher {
Expand All @@ -512,12 +515,12 @@ impl Philosopher {

fn eat(&self, table: &Table) {
let _left = table.forks[self.left].lock().unwrap();
thread::sleep_ms(150);
thread::sleep(Duration::from_millis(150));
let _right = table.forks[self.right].lock().unwrap();

println!("{} is eating.", self.name);

thread::sleep_ms(1000);
thread::sleep(Duration::from_millis(1000));

println!("{} is done eating.", self.name);
}
Expand Down Expand Up @@ -598,12 +601,12 @@ We now need to construct those `left` and `right` values, so we add them to
```rust,ignore
fn eat(&self, table: &Table) {
let _left = table.forks[self.left].lock().unwrap();
thread::sleep_ms(150);
thread::sleep(Duration::from_millis(150));
let _right = table.forks[self.right].lock().unwrap();
println!("{} is eating.", self.name);
thread::sleep_ms(1000);
thread::sleep(Duration::from_millis(1000));
println!("{} is done eating.", self.name);
}
Expand All @@ -614,8 +617,8 @@ We have three new lines. We’ve added an argument, `table`. We access the
the fork at that particular index. That gives us access to the `Mutex` at that
index, and we call `lock()` on it. If the mutex is currently being accessed by
someone else, we’ll block until it becomes available. We have also a call to
`thread::sleep_ms` between the moment first fork is picked and the moment the
second forked is picked, as the process of picking up the fork is not
`thread::sleep` between the moment the first fork is picked and the moment the
second forked is picked, as the process of picking up the fork is not
immediate.

The call to `lock()` might fail, and if it does, we want to crash. In this
Expand Down

0 comments on commit be4cd07

Please sign in to comment.