Skip to content

w1adek/dining-philosophers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Dining Philosophers (C++17, Monitor Solution)

This project implements the classic Dining Philosophers concurrency problem using:

  • std::thread for philosopher threads,
  • a monitor-style synchronization class (Monitor),
  • std::mutex + std::condition_variable for 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.

How it works

Each philosopher runs in its own thread and repeats this cycle:

  1. Think
  2. Ask monitor to pick up forks (PickUp)
  3. Eat
  4. 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.

Requirements

  • CMake >= 3.10
  • C++17-compatible compiler (e.g. g++, clang++, or MSVC)

Build and run (Windows PowerShell)

From the project root:

cmake -S . -B build
cmake --build build
.
\build\dining_philosophers.exe

Build and run (Linux/macOS)

cmake -S . -B build
cmake --build build
./build/dining_philosophers

About

This project implements the classic Dining Philosophers concurrency problem using Monitors

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors