Easy C++ using condition variable and two atomic counters [16 ms, 99.78%]
Each thread increases a counter of how many times it has printed.\nEach thread also checks the other thread\'s counter to see whether it is high enough for this
Each thread increases a counter of how many times it has printed.\nEach thread also checks the other thread\'s counter to see whether it is high enough for this thread to go ahead and print another time. If it isn\'t high enough, the thread waits until it is signalled by the other thread through their shared condition var to tell it that the count now is high enough.\nWhen a thread has finished its print and inced its counter, it uses the condition variable to wake up the other thread if it was sleeping.\nMost of the time the single mutex is not locked. It only gets locked to protect the condition varible as a wait is started.\nIf we had hardware concurrency > 1 a simple busy loop would win but since we don\'t, waiting and signalling is a better choice.\n\n# Code\n```c++\n     class FooBar {\n        const unsigned n;\n        atomic<unsigned> foosDone = 0;\n        atomic<unsigned> barsDone = 0;\n        mutex m;\n        condition_variable cv;\n\t\t\n    public:\n        FooBar(unsigned n_) : n(n_){\n        }\n\n        void foo(function<void()> printFoo) {\n\n            for (int i = 0; i < n; i++) {\n                {\n                    unique_lock l(m);\n                    cv.wait(l, [this, &i]{return barsDone >= i;});\n                }\n                // printFoo() outputs "foo". Do not change or remove this line.\n                printFoo();\n                ++foosDone;\n                cv.notify_one();\n            }\n        }\n\n        void bar(function<void()> printBar) {\n\n            for (int i = 0; i < n; i++) {\n                {\n                    unique_lock l(m);\n                    cv.wait(l, [this, &i]{return foosDone > i;});\n                }\n                // printBar() outputs "bar". Do not change or remove this line.\n                printBar();\n                ++barsDone;\n                cv.notify_one();\n            }\n        }\n    };\n```\n\n# Results\nFirst run:\n>Runtime: 16 ms, faster than 99.78% of C++ online submissions for Print FooBar Alternately.\n>Memory Usage: 10.6 MB, less than 100.00% of C++ online submissions for Print FooBar Alternately.\n\nSecond run (WTF):\n\n>Runtime: 344 ms, faster than 64.05% of C++ online submissions for Print FooBar Alternately.\n>Memory Usage: 10.5 MB, less than 100.00% of C++ online submissions for Print FooBar Alternately.
['C']