C++ Solution using a semaphore for each philosopher
\n#include <semaphore.h>\n\nclass DiningPhilosophers \n{\npublic:\n    DiningPhilosophers() \n    {\n        for(int i = 0; i < 5; i++)\n        {\n            
```\n#include <semaphore.h>\n\nclass DiningPhilosophers \n{\npublic:\n    DiningPhilosophers() \n    {\n        for(int i = 0; i < 5; i++)\n        {\n            sem_t sem;\n            sem_init(&sem, 0, 1);\n            forks.push_back(sem);\n        }\n    }\n    \n    ~DiningPhilosophers()\n    {\n\t\t//destroy semaphores\n        for(int i = 0; i < 5; i++)\n        {\n            sem_destroy(&forks[i]);\n        }\n    }\n    \n    void wantsToEat(int philosopher, function<void()> pickLeftFork, function<void()> pickRightFork, function<void()> eat, function<void()> putLeftFork, function<void()> putRightFork) \n    {\n            //wait for right fork\n            sem_wait(&forks[philosopher]);\n            //wait for left fork\n            sem_wait(&forks[(philosopher + 1) % 5]);\n        \n            pickLeftFork();\n            pickRightFork();\n            eat();\n            putLeftFork(); \n            putRightFork();\n        \n            sem_post(&forks[philosopher]);\n            sem_post(&forks[(philosopher + 1) % 5]);\n\t\t\n    }\nprivate:\n    vector<sem_t> forks;\n};\n```
[]