sleep_ms -> sleep

Now that thread::sleep is a real thing, let's use it

Fixes #29621
This commit is contained in:
Steve Klabnik 2015-11-05 17:29:36 +01:00
parent a216e84727
commit 801f83ff65
2 changed files with 21 additions and 14 deletions

View File

@ -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];
@ -131,7 +132,7 @@ fn main() {
});
}
thread::sleep_ms(50);
thread::sleep(Duration::from_millis(50));
}
```
@ -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]);
@ -176,7 +178,7 @@ fn main() {
});
}
thread::sleep_ms(50);
thread::sleep(Duration::from_millis(50));
}
```
@ -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]));
@ -219,7 +222,7 @@ fn main() {
});
}
thread::sleep_ms(50);
thread::sleep(Duration::from_millis(50));
}
```
@ -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 {
@ -250,7 +254,7 @@ thread::spawn(move || {
data[i] += 1;
});
# }
# thread::sleep_ms(50);
# thread::sleep(Duration::from_millis(50));
# }
```

View File

@ -264,6 +264,7 @@ eat. Heres the next version:
```rust
use std::thread;
use std::time::Duration;
struct Philosopher {
name: String,
@ -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);
}
@ -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:
@ -345,6 +346,7 @@ Heres the next iteration:
```rust
use std::thread;
use std::time::Duration;
struct Philosopher {
name: String,
@ -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);
}
@ -493,6 +495,7 @@ Lets modify the program to use the `Table`:
```rust
use std::thread;
use std::time::Duration;
use std::sync::{Mutex, Arc};
struct Philosopher {
@ -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);
}
@ -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);
}
@ -614,8 +617,8 @@ We have three new lines. Weve 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, well 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