2

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?

Sep Roland
  • 33,889
  • 7
  • 43
  • 76
Acapello
  • 127
  • 9
  • 1
    I am envisioning some sort of queue. You have all the philosophers in the queue. When you have an open thread you pop the philosophers out of the queue and give it to the thread. Once the thread is done you add the philosophers to the end of the queue. This way you would keep cycling through the philosophers and it should pretty fair. – NathanOliver Feb 22 '16 at 21:04
  • What is your C++ question? I don't se any relation to it... – Ulrich Eckhardt Feb 22 '16 at 21:13
  • This is a pretty broad question, you're basically asking for a better solution to the dining philosophers program – M.M Feb 22 '16 at 21:15
  • 1
    to coordinate a 'fair' startup of multiple threads, I have all the threads start, but do no part of sharing the meal until a shared "go" flag is set by main ... each thread spin waits (in ubuntu) using usleep(1000) until flag is set. Such spin waits are very light duty on the cores, and results in a much fairer synchronized start. – 2785528 Feb 22 '16 at 21:17
  • Unfortunately, I can't post my code due to competiton rules of my teacher. But can ask for advice. @inetknght – Acapello Feb 22 '16 at 21:48
  • @M.M I understand that it is a broad question. But no, I don't asking for better solution. I have my correct solution and only want to improve it in questions of speed and fairness. – Acapello Feb 22 '16 at 21:52
  • @douglas-o-moen Thank you for your advisc! I'll try it:) – Acapello Feb 22 '16 at 21:52
  • The whole point of the dining philosophers problem is to optimize the speed and fairness (after you've avoided basic errors like deadlocks, of course). Improving those things is equivalent to finding a better solution. – M.M Feb 22 '16 at 21:58
  • Its depends heavily on how you've avoided deadlock. If you done it generically by prioritising all the mutexes, that is inherently unfair; philosopers whith lower priority mutexes will eat less often than those with higher priority. – Chris Dodd May 16 '20 at 21:28

0 Answers0