an easy c++ solution using two mutex
\nclass FooBar {\nprivate:\n    int n;\n    mutex m1, m2;\n\npublic:\n    FooBar(int n) {\n        this->n = n;\n        m2.lock();\n    }\n\n    void foo(funct
```\nclass FooBar {\nprivate:\n    int n;\n    mutex m1, m2;\n\npublic:\n    FooBar(int n) {\n        this->n = n;\n        m2.lock();\n    }\n\n    void foo(function<void()> printFoo) {\n        for (int i = 0; i < n; i++) {\n            m1.lock();\n        \t// printFoo() outputs "foo". Do not change or remove this line.\n        \tprintFoo();\n            m2.unlock();\n        }\n    }\n\n    void bar(function<void()> printBar) {\n        for (int i = 0; i < n; i++) {\n            m2.lock();\n        \t// printBar() outputs "bar". Do not change or remove this line.\n        \tprintBar();\n            m1.unlock();\n        }\n    }\n};\n```
[]