I have a task to solve the Dining Philosophers problem. I have realised the logic of the program. It is correct based on tests.
However with a great number of philosophers (>2000 threads) and for a short work time (5 sec) it's becoming unfair. The first philosophers eat more often than the philosophers at the end. I think the reason is in the order of turning on.
My realisation is based on mutex
and std::atomic
. To avoid active waiting (constantly trying trylock()
) I placed wait(sleep_time)
.
First question: would it be faster if my realisation were based on condition variables?
Second question: how to avoid unfair behaviour due to the time needed to start a lot of threads? Maybe start them in random order?