Skip to content

Commit 512878a

Browse files
committed
wip: async dining philosophers simplification
1 parent 9412254 commit 512878a

File tree

1 file changed

+4
-15
lines changed

1 file changed

+4
-15
lines changed

src/concurrency/async-exercises/dining-philosophers.rs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,11 @@ impl Philosopher {
4444
// Keep trying until we have both forks
4545
// ANCHOR_END: Philosopher-eat
4646
let (_left_fork, _right_fork) = loop {
47+
tokio::task::yield_now().await;
4748
// Pick up forks...
48-
let left_fork = self.left_fork.try_lock();
49-
let right_fork = self.right_fork.try_lock();
50-
let Ok(left_fork) = left_fork else {
51-
// If we didn't get the left fork, drop the right fork if we
52-
// have it and let other tasks make progress.
53-
drop(right_fork);
54-
time::sleep(time::Duration::from_millis(1)).await;
55-
continue;
56-
};
57-
let Ok(right_fork) = right_fork else {
58-
// If we didn't get the right fork, drop the left fork and let
59-
// other tasks make progress.
60-
drop(left_fork);
61-
time::sleep(time::Duration::from_millis(1)).await;
62-
continue;
49+
match (self.left_fork.try_lock(), self.right_fork.try_lock()) {
50+
(Ok(left_fork), Ok(right_fork)) => (left_fork, right_fork),
51+
(_, _) => continue,
6352
};
6453
break (left_fork, right_fork);
6554
};

0 commit comments

Comments
 (0)