From the course: Complete Guide to Parallel and Concurrent Programming with C++

Unlock this course with a free trial

Join today to access over 24,900 courses taught by industry experts.

Starvation: C++ demo

Starvation: C++ demo

- [Instructor] To demonstrate thread starvation, we'll modify this version of the dining philosopher's example program to add a local variable within the philosopher function to keep track of how many pieces of sushi each philosopher thread gets to eat. We'll increment the philosopher's sushi eaten variable after they take a piece of sushi on line 15. And then at the end, after their while loop finishes, we'll print a message with the number of pieces this philosopher was able to take. There are currently 5,000 pieces of sushi up for grabs on line seven, so I'll switch over to the command prompt, make, and then run this program. And when it finishes, I can see that each philosopher got a different amount of sushi. It's not necessarily fair, but there's plenty of sushi to go around, so both of the philosophers are well fed. Now let's see what happens if we drastically increase the number of philosophers at the dinner party by modifying the array to hold philosopher threads on line 23…

Contents