This project implements the classic Dining Philosophers concurrency problem using:
std::threadfor philosopher threads,- a monitor-style synchronization class (
Monitor), std::mutex+std::condition_variablefor safe coordination.
The simulation runs continuously and prints philosopher states (THINKING, HUNGRY, EATING) in the terminal; stop with Ctrl+C. ANSI color escape codes are used in terminal output.
Each philosopher runs in its own thread and repeats this cycle:
- Think
- Ask monitor to pick up forks (
PickUp) - Eat
- Put down forks (
PutDown)
The monitor ensures that adjacent philosophers cannot eat at the same time, but strict fairness is not explicitly enforced. When a philosopher becomes hungry, it waits on its own condition variable until both neighbors are not eating.
- CMake
>= 3.10 - C++17-compatible compiler (e.g.
g++,clang++, or MSVC)
From the project root:
cmake -S . -B build
cmake --build build
.
\build\dining_philosophers.execmake -S . -B build
cmake --build build
./build/dining_philosophers