// Exceptionally polite philosophers, thinking and eating sushi
#include <mutex>
#include <thread>
int sushi_count = 5000;
void philosopher(std::mutex& first_chopstick, std::mutex& second_chopstick) {
while (sushi_count > 0) {
first_chopstick.lock();
if (!second_chopstick.try_lock()) {
first_chopstick.unlock();
// allow current thread to wait a moment (don't pick the first_chopstick
// so fast, so other threads can have chances to pick the first_chopstick)
std::this_thread::yield();
} else {
if (sushi_count) {
--sushi_count;
}
second_chopstick.unlock();
first_chopstick.unlock();
}
}
}
int main() {
std::mutex chopstick_a, chopstick_b;
std::thread barron(philosopher, std::ref(chopstick_a), std::ref(chopstick_b));
std::thread olivia(philosopher, std::ref(chopstick_b), std::ref(chopstick_a));
std::thread steve(philosopher, std::ref(chopstick_a), std::ref(chopstick_b));
std::thread nikki(philosopher, std::ref(chopstick_b), std::ref(chopstick_a));
barron.join();
olivia.join();
steve.join();
nikki.join();
printf("The philosophers are done eating.\n");
}