C++ CV + Mutex
\n\n# Code\n\nclass FooBar {\nprivate:\n    int n;\n\npublic:\n    FooBar(int n) {\n        this->n = n;\n    }\n\n    void foo(function<void()> printFoo) {\n  
\n\n# Code\n```\nclass FooBar {\nprivate:\n    int n;\n\npublic:\n    FooBar(int n) {\n        this->n = n;\n    }\n\n    void foo(function<void()> printFoo) {\n        \n        for (int i = 0; i < n; i++) {\n            {\n                std::unique_lock lg(mut);\n                while(foo_done)\n                    cv.wait(lg);\n\n        \t    // printFoo() outputs "foo". Do not change or remove this line.\n        \t    printFoo();\n                foo_done = true;\n            }\n            cv.notify_one();\n        }\n    }\n\n    void bar(function<void()> printBar) {\n        \n        for (int i = 0; i < n; i++) {\n            \n            {\n                std::unique_lock ul(mut);\n                while(!foo_done)\n                    cv.wait(ul);\n        \t    // printBar() outputs "bar". Do not change or remove this line.\n        \t    printBar();\n                foo_done = false;\n            }\n            cv.notify_one();\n        }\n    }\n\nprivate:\n    bool foo_done = false;\n    std::mutex mut;\n    std::condition_variable cv;\n};\n```
['C++']