Dining philosphers || semaphores || C++
\n// implimentation of semaphore\nclass semaphore{\n    public:\n    semaphore(){}\n    semaphore(int c) : count(c){};\n    void setcount(int a)\n    { count= a
```\n// implimentation of semaphore\nclass semaphore{\n    public:\n    semaphore(){}\n    semaphore(int c) : count(c){};\n    void setcount(int a)\n    { count= a;}\n    inline void signal()\n    {\n        std::unique_lock<std::mutex>lock(mtx);\n        count++;\n        if(count<=0)\n            cv.notify_one();\n       \n    }\n     inline void wait()\n        {\n            std::unique_lock<std::mutex>lock(mtx);\n        count--;\n        while(count<0)\n           cv.wait(lock);\n        }\n    private:\n    std::mutex mtx;\n    std::condition_variable cv;\n    int count;\n};\nclass DiningPhilosophers {\n    semaphore fork[5];\n    std::mutex m,l;\npublic:\n    DiningPhilosophers() {\n        for(int i=0;i<5;i++)\n        {\n            fork[i].setcount(1);\n        }\n        \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        {  std::lock_guard<std::mutex>lock(m);\n        fork[(philosopher+1)%5].wait();\n         fork[philosopher].wait();\n\t\t\n    }\n     pickLeftFork();\n    pickRightFork();\n    eat();\n    putLeftFork();\n    fork[(philosopher+1)%5].signal();\n    putRightFork();\n         fork[philosopher].signal();\n    }\n};\n\n\n\n\n\n\n\n\n\n\n\n\n\n```
['C', 'C++']