C++ RAII Styled Locking 12ms faster than 99.60
We only use cond notify one to reduce contention grabbing the mutex lock. Makes use of raii locking. \n\nOther alternatives:\n- use a binary sempahore for signa
We only use cond notify one to reduce contention grabbing the mutex lock. Makes use of raii locking. \n\nOther alternatives:\n- use a binary sempahore for signaling. \n- without mutex and cond you can do spin lock with atomic bool\n\n```\nclass FooBar {\nprivate:\n    int n;\n    mutex m;\n    condition_variable cond;\n    bool isFoo;\n\npublic:\n    FooBar(int n) {\n        this->n = n;\n        isFoo = true;\n    }\n\n    void foo(function<void()> printFoo) {\n        \n        for (int i = 0; i < n; i++) {\n            \n            {\n                unique_lock<mutex> lk(m);\n                cond.wait(lk, [this] { return isFoo; });\n        \t    // printFoo() outputs "foo". Do not change or remove this line.\n        \t    printFoo();\n                isFoo = false;\n            }\n            cond.notify_one();\n        }\n    }\n\n    void bar(function<void()> printBar) {\n        \n        for (int i = 0; i < n; i++) {\n            \n            {\n                unique_lock<mutex> lk(m);\n                cond.wait(lk, [this] { return isFoo==false; });\n        \t    // printBar() outputs "bar". Do not change or remove this line.\n        \t    printBar();\n                isFoo = true;\n            }\n            cond.notify_one();\n        }\n    }\n};\n```
[]