C++ Ultra Simple Solution with Mutexes
\nclass FooBar {\nprivate:\n    int n;\n    \n    std::mutex foo_gate;\n    std::mutex bar_gate;\n\npublic:\n    FooBar(int new_n) : n(new_n) {\n        bar_gat
```\nclass FooBar {\nprivate:\n    int n;\n    \n    std::mutex foo_gate;\n    std::mutex bar_gate;\n\npublic:\n    FooBar(int new_n) : n(new_n) {\n        bar_gate.lock();\n    }\n\n    void foo(function<void()> printFoo) {\n        for (int i = 0; i < n; i++) {\n            foo_gate.lock();\n        \tprintFoo();\n            bar_gate.unlock();\n        }\n    }\n\n    void bar(function<void()> printBar) {\n        for (int i = 0; i < n; i++) {\n            bar_gate.lock();\n        \tprintBar();\n            foo_gate.unlock();\n        }\n    }\n};\n```
[]