@@ -264,6 +264,7 @@ eat. Here’s the next version:
264
264
265
265
``` rust
266
266
use std :: thread;
267
+ use std :: time :: Duration ;
267
268
268
269
struct Philosopher {
269
270
name : String ,
@@ -279,7 +280,7 @@ impl Philosopher {
279
280
fn eat (& self ) {
280
281
println! (" {} is eating." , self . name);
281
282
282
- thread :: sleep_ms ( 1000 );
283
+ thread :: sleep ( Duration :: from_millis ( 1000 ) );
283
284
284
285
println! (" {} is done eating." , self . name);
285
286
}
@@ -313,13 +314,13 @@ from the standard library, and so we need to `use` it.
313
314
fn eat(&self) {
314
315
println!("{} is eating.", self.name);
315
316
316
- thread::sleep_ms( 1000);
317
+ thread::sleep(Duration::from_millis( 1000) );
317
318
318
319
println!("{} is done eating.", self.name);
319
320
}
320
321
```
321
322
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
323
324
simulate the time it takes a philosopher to eat.
324
325
325
326
If you run this program, you should see each philosopher eat in turn:
@@ -345,6 +346,7 @@ Here’s the next iteration:
345
346
346
347
``` rust
347
348
use std :: thread;
349
+ use std :: time :: Duration ;
348
350
349
351
struct Philosopher {
350
352
name : String ,
@@ -360,7 +362,7 @@ impl Philosopher {
360
362
fn eat (& self ) {
361
363
println! (" {} is eating." , self . name);
362
364
363
- thread :: sleep_ms ( 1000 );
365
+ thread :: sleep ( Duration :: from_millis ( 1000 ) );
364
366
365
367
println! (" {} is done eating." , self . name);
366
368
}
@@ -493,6 +495,7 @@ Let’s modify the program to use the `Table`:
493
495
494
496
``` rust
495
497
use std :: thread;
498
+ use std :: time :: Duration ;
496
499
use std :: sync :: {Mutex , Arc };
497
500
498
501
struct Philosopher {
@@ -512,12 +515,12 @@ impl Philosopher {
512
515
513
516
fn eat (& self , table : & Table ) {
514
517
let _left = table . forks[self . left]. lock (). unwrap ();
515
- thread :: sleep_ms ( 150 );
518
+ thread :: sleep ( Duration :: from_millis ( 150 ) );
516
519
let _right = table . forks[self . right]. lock (). unwrap ();
517
520
518
521
println! (" {} is eating." , self . name);
519
522
520
- thread :: sleep_ms ( 1000 );
523
+ thread :: sleep ( Duration :: from_millis ( 1000 ) );
521
524
522
525
println! (" {} is done eating." , self . name);
523
526
}
@@ -598,12 +601,12 @@ We now need to construct those `left` and `right` values, so we add them to
598
601
``` rust,ignore
599
602
fn eat(&self, table: &Table) {
600
603
let _left = table.forks[self.left].lock().unwrap();
601
- thread::sleep_ms( 150);
604
+ thread::sleep(Duration::from_millis( 150) );
602
605
let _right = table.forks[self.right].lock().unwrap();
603
606
604
607
println!("{} is eating.", self.name);
605
608
606
- thread::sleep_ms( 1000);
609
+ thread::sleep(Duration::from_millis( 1000) );
607
610
608
611
println!("{} is done eating.", self.name);
609
612
}
@@ -614,8 +617,8 @@ We have three new lines. We’ve added an argument, `table`. We access the
614
617
the fork at that particular index. That gives us access to the ` Mutex ` at that
615
618
index, and we call ` lock() ` on it. If the mutex is currently being accessed by
616
619
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
619
622
immediate.
620
623
621
624
The call to ` lock() ` might fail, and if it does, we want to crash. In this
0 commit comments