C++ take two locks at same time and defer taking locks while creation
Here we have exclusivity and ownership to a fork when someone picks it, so created 5 mutexes.\nWhen ever someone wants to eat they will try to lock both at same
Here we have exclusivity and ownership to a fork when someone picks it, so created 5 mutexes.\nWhen ever someone wants to eat they will try to lock both at same time to avoid dead lock scenario.\nwhile creating ul1,ul2 I did a defer lock so it wont be locked in constructor and locked them in a single and that will take care of either locking both or not.\n```\nclass DiningPhilosophers {\n    mutex m[5];\npublic:\n    DiningPhilosophers() {\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\t\tunique_lock<mutex> ul1(m[philosopher], std::defer_lock), ul2(m[(philosopher+1)%5],std::defer_lock);\n\t\t// Locking at same time to avoid deadlock\n        lock(ul1,ul2);\n        pickLeftFork();pickRightFork();eat();putLeftFork();putRightFork();\n    }\n};\n```
[]