hey, I am trying solving the phiplosopher dinning ...
# coroutines
k
hey, I am trying solving the phiplosopher dinning problem, and this is my code but there is a dead lock, the log shows
Copy code
Philosopher2 takes left fork 2
Philosopher0 takes left fork 0
Philosopher1 takes left fork 1
Philosopher3 takes left fork 3
Philosopher4 takes left fork 4
how can I fix it ?
s
The dining philosophers problem is designed to demonstrate deadlocks like this. Do you have a solution in mind that you're trying to implement in Kotlin, or are you just asking about how to solve the deadlock in general? If it's the latter, you need to start by studying the existing material that's been written about this problem, rather than asking here.
k
actually I have solve it in java using the lock, but I want to use coroutines, so I ask it here
ok, I solve it
but I think it is possible that there will be a dead lock, and I also want to use
select
to solve it
u
I think you should be safe, as you are always receiving forks in order (i.e. left fork first which is higher in your forks array then the right fork)