This project implements the classic Dining Philosophers Problem in safe, idiomatic Rust using threads, Arc, and Mutex. It simulates philosophers thinking and eating with shared forks, preventing deadlocks via ordered locking.
- Thread-safe
ForkPoolmanaging shared forks - Deadlock-free fork acquisition logic (even/odd ordering)
- Configurable eating cycles per philosopher
- Inter-thread communication with
mpscchannel to verify philosopher activity - Modular and testable architecture
- Deadlock detection test with timeout
Each philosopher is modeled as a thread.
- Forks are shared via
Arc<Mutex<T>> - Philosophers alternate between thinking and eating
- To prevent deadlock:
- Even philosophers lock left → right
- Odd philosophers lock right → left
src/
├── lib.rs # All logic and corresponding unit tests
├── main.rs # Command line utility to test the core logic
You can run the simulation by calling:
run_philosophers(); // 5 philosophers, 3 eating cycles eachThis project includes a comprehensive test suite under #[cfg(test)].
✅ Fork Pair Consistency
- Ensures left/right forks are never the same
✅ Run Completes
- Runs all philosophers and checks for thread errors
✅ Single Philosopher
- Verifies one thread can acquire forks and eat
✅ Deadlock Detection
- Simulates runtime and checks for timeout failure
cargo test- No unsafe code
- Uses
Arc<Mutex<T>>safely - Tests include deadlock timeout check
This project helps you practice:
- Learning Rust concurrency
- Safe multithreading with
ArcandMutex - Deadlock-free resource coordination
- Writing concurrent tests in Rust
- Structuring clean, idiomatic Rust libraries
MIT License © 2025 VG