✅Using Mutex ✅|| Lock || 🏆Easy to understand 🏆
\n\n# Code\n\nclass FooBar {\nprivate:\n    int n;\n    std:: mutex m ;\n    std:: condition_variable cv;\n    bool turn;\npublic:\n    FooBar(int n) {\n       
\n\n# Code\n```\nclass FooBar {\nprivate:\n    int n;\n    std:: mutex m ;\n    std:: condition_variable cv;\n    bool turn;\npublic:\n    FooBar(int n) {\n        this->n = n;\n        turn = 0 ;\n    }\n\n    void foo(function<void()> printFoo) {\n        \n        for (int i = 0; i < n; i++) {\n            std:: unique_lock<std:: mutex > lock(m);\n            while(turn ==1){\n                cv.wait(lock);\n            }\n        \t// printFoo() outputs "foo". Do not change or remove this line.\n        \tprintFoo();\n            turn = 1;\n            cv.notify_all();\n        }\n    }\n\n    void bar(function<void()> printBar) {\n        \n        for (int i = 0; i < n; i++) {\n            std::unique_lock< std:: mutex> lock(m);\n            while(turn == 0){\n                cv.wait(lock);\n            }\n        \t// printBar() outputs "bar". Do not change or remove this line.\n        \tprintBar();\n            turn = 0;\n            cv.notify_all();\n        }\n    }\n};\n```
['C++']