c++
there are three types of solution:\n(a) we only control 4 peoples can  dine  at the same time,  they can finish the ding one by one.\n(b) people who ding must p
1. there are three types of solution:\n(a) we only control 4 peoples can  dine  at the same time,  they can finish the ding one by one.\n(b) people who ding must pick up the left and right forks at the same time, lacking one of them can not finish  dining.\n(c) the ord of picking up fork should follow the rule\uFF1Athe odd index people should pick up the right  fork first, the even index people should pick up the left fork first.\n```\nclass Semaphore {\npublic:\n  Semaphore(int count = 0) : count_(count) {\n  }\n    \n  void Set(int count){\n      count_ = count;\n  }\n    \n  void Signal() {\n    std::unique_lock<std::mutex> lock(mutex_);\n    ++count_;\n    cv_.notify_one();\n  }\n\n  void Wait() {\n    std::unique_lock<std::mutex> lock(mutex_);\n    while(count_ <= 0){\n        cv_.wait(lock);\n    }\n    --count_;\n  }\n\nprivate:\n  std::mutex mutex_;\n  std::condition_variable cv_;\n  int count_;\n};\n\nclass DiningPhilosophers {\npublic:\n    DiningPhilosophers() {\n        guid.Set(4);\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        int l = philosopher;\n        int r = (philosopher+1)%5;\n        guid.Wait();        \n        \n        lock[l].lock();\n        lock[r].lock();\n        pickLeftFork();\n        pickRightFork();\n        eat();\n        putRightFork();\n        putLeftFork();\n        lock[r].unlock();\n        lock[l].unlock();\n        \n        guid.Signal();\n    }\nprivate:\n    std::mutex lock[5];\n    Semaphore guid;\n};\n```\n```\nclass DiningPhilosophers {\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        int l = philosopher;\n        int r = (philosopher+1)%5;\n        guid.lock();        \n        lock[l].lock();\n        lock[r].lock();\n        pickLeftFork();\n        pickRightFork();\n        guid.unlock();\n        eat();\n        putRightFork();\n        putLeftFork();\n        lock[l].unlock();\n        lock[r].unlock();\n    }\nprivate:\n    std::mutex lock[5];\n    std::mutex guid;\n};\n```\n```\nclass DiningPhilosophers {\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        int l = philosopher;\n        int r = (philosopher+1)%5;\n        if(philosopher%2 == 0){\n            lock[r].lock();\n            lock[l].lock();\n            pickLeftFork();\n            pickRightFork();\n        }else{\n            lock[l].lock();\n            lock[r].lock();\n            pickLeftFork();\n            pickRightFork();\n        }\n        \n        eat();\n        putRightFork();\n        putLeftFork();\n        lock[l].unlock();\n        lock[r].unlock();\n    }\nprivate:\n    std::mutex lock[5];\n};\n```
[]