Skip to content

Commit be4cd07

Browse files
committed
Rollup merge of rust-lang#29622 - steveklabnik:gh29621, r=bstrie
Now that thread::sleep is a real thing, let's use it Fixes rust-lang#29621 r? @bstrie
2 parents 50e8707 + 801f83f commit be4cd07

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

src/doc/trpl/concurrency.md

+8-4
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ languages. It will not compile:
121121

122122
```ignore
123123
use std::thread;
124+
use std::time::Duration;
124125
125126
fn main() {
126127
let mut data = vec![1, 2, 3];
@@ -131,7 +132,7 @@ fn main() {
131132
});
132133
}
133134
134-
thread::sleep_ms(50);
135+
thread::sleep(Duration::from_millis(50));
135136
}
136137
```
137138

@@ -165,6 +166,7 @@ indivisible operations which can't have data races.
165166
```ignore
166167
use std::thread;
167168
use std::sync::Arc;
169+
use std::time::Duration;
168170
169171
fn main() {
170172
let mut data = Arc::new(vec![1, 2, 3]);
@@ -176,7 +178,7 @@ fn main() {
176178
});
177179
}
178180
179-
thread::sleep_ms(50);
181+
thread::sleep(Duration::from_millis(50));
180182
}
181183
```
182184

@@ -207,6 +209,7 @@ Here's the working version:
207209
```rust
208210
use std::sync::{Arc, Mutex};
209211
use std::thread;
212+
use std::time::Duration;
210213

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

222-
thread::sleep_ms(50);
225+
thread::sleep(Duration::from_millis(50));
223226
}
224227
```
225228

@@ -241,6 +244,7 @@ Let's examine the body of the thread more closely:
241244
```rust
242245
# use std::sync::{Arc, Mutex};
243246
# use std::thread;
247+
# use std::time::Duration;
244248
# fn main() {
245249
# let data = Arc::new(Mutex::new(vec![1, 2, 3]));
246250
# for i in 0..3 {
@@ -250,7 +254,7 @@ thread::spawn(move || {
250254
data[i] += 1;
251255
});
252256
# }
253-
# thread::sleep_ms(50);
257+
# thread::sleep(Duration::from_millis(50));
254258
# }
255259
```
256260

src/doc/trpl/dining-philosophers.md

+13-10
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ eat. Here’s the next version:
264264

265265
```rust
266266
use std::thread;
267+
use std::time::Duration;
267268

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

282-
thread::sleep_ms(1000);
283+
thread::sleep(Duration::from_millis(1000));
283284

284285
println!("{} is done eating.", self.name);
285286
}
@@ -313,13 +314,13 @@ from the standard library, and so we need to `use` it.
313314
fn eat(&self) {
314315
println!("{} is eating.", self.name);
315316
316-
thread::sleep_ms(1000);
317+
thread::sleep(Duration::from_millis(1000));
317318
318319
println!("{} is done eating.", self.name);
319320
}
320321
```
321322

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

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

346347
```rust
347348
use std::thread;
349+
use std::time::Duration;
348350

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

363-
thread::sleep_ms(1000);
365+
thread::sleep(Duration::from_millis(1000));
364366

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

494496
```rust
495497
use std::thread;
498+
use std::time::Duration;
496499
use std::sync::{Mutex, Arc};
497500

498501
struct Philosopher {
@@ -512,12 +515,12 @@ impl Philosopher {
512515

513516
fn eat(&self, table: &Table) {
514517
let _left = table.forks[self.left].lock().unwrap();
515-
thread::sleep_ms(150);
518+
thread::sleep(Duration::from_millis(150));
516519
let _right = table.forks[self.right].lock().unwrap();
517520

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

520-
thread::sleep_ms(1000);
523+
thread::sleep(Duration::from_millis(1000));
521524

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

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

0 commit comments

Comments
 (0)