C++ ||  Using semaphores and mutexs
\ntemplate<int COUNT_MAX>\nclass counting_semaphore\n{\n    std::atomic<int> val_ {};\n  public:\n    \n    counting_semaphore(int val = 0): val_(val)\n    {\n 
```\ntemplate<int COUNT_MAX>\nclass counting_semaphore\n{\n    std::atomic<int> val_ {};\n  public:\n    \n    counting_semaphore(int val = 0): val_(val)\n    {\n        if (val_ < 0) val_ = 0;\n        else if (val_ > COUNT_MAX) val_ = COUNT_MAX;\n    }\n    \n    void acquire()\n    {\n        while (val_ == 0) std::this_thread::yield();\n        --val_;    \n    }\n    \n    void release()\n    {\n        if (val_ < COUNT_MAX)\n            ++val_;\n    }\n};\n\nclass DiningPhilosophers {\n    \n    std::array<std::mutex, 5> forks;\n    counting_semaphore<4> sem {4};\npublic:\n    DiningPhilosophers() {\n    }\n\n    void wantsToEat(int philosopher,\n                    function<void()> pickLeftFork,\n                    function<void()> pickRightFork,\n                    function<void()> eat,\n                    function<void()> putLeftFork,\n                    function<void()> putRightFork) {\n\t    int left = philosopher;\n        int right = (philosopher + 1) % 5;\n        \n        sem.acquire();\n        std::lock_guard<std::mutex> l(forks[left]);\n        std::lock_guard<std::mutex> r(forks[right]);\n        pickLeftFork();\n        pickRightFork();\n        eat();\n        putLeftFork();\n        putRightFork();\n        sem.release();\n    }\n};\n```
[]