C++, 104 ms, 10.5 MB
Please, note : solution with mutex+condition_variable is 2-3x slower comparing with atomic.\n\n\nclass FooBar {\nprivate:\n    int n;\n    std::atomic<bool> b{t
Please, note : solution with mutex+condition_variable is 2-3x slower comparing with atomic.\n\n```\nclass FooBar {\nprivate:\n    int n;\n    std::atomic<bool> b{true};\n\npublic:\n    FooBar(int n) {\n        this->n = n;\n    }\n\n    void foo(std::function<void()> printFoo)\n    {\n        for (int i = 0; i < n; i++)\n        {\n            while (!b.load(std::memory_order_acquire))\n            {\n                std::this_thread::yield();\n            };\n            printFoo();\n            b.store(false, std::memory_order_release);\n        }\n    }\n\n    void bar(std::function<void()> printBar)\n    {\n        for (int i = 0; i < n; i++)\n        {\n            while (b.load(std::memory_order_acquire))\n            {\n                std::this_thread::yield();\n            };\n            printBar();\n            b.store(true, std::memory_order_release);\n        }\n    }\n};\n```
[]