C++ mutex and condition_variable
Semaphore implementation with mutex and condition variable from stack overflow. For the people who don\'t want to use posix semaphores.\n\nclass Semaphore {\npu
Semaphore implementation with mutex and condition variable from stack overflow. For the people who don\'t want to use posix semaphores.\n```\nclass Semaphore {\npublic:\n    Semaphore (bool towait_)\n        : towait(towait_) {}\n\n    inline void notify()\n    {\n        std::unique_lock<std::mutex> lock(mtx);\n        towait = false;\n        cv.notify_one();\n    }\n\n    inline void wait()\n    {\n        std::unique_lock<std::mutex> lock(mtx);\n        cv.wait(lock,[&](){return !towait;});\n        towait = true;\n    }\n\nprivate:\n    std::mutex mtx;\n    std::condition_variable cv;\n    bool towait;\n};\n\nclass FooBar {\nprivate:\n    int n;\n    Semaphore semFoo{false};\n    Semaphore semBar{true}; \npublic:\n    FooBar(int n) {\n        this->n = n;\n    }\n\n    void foo(function<void()> printFoo) {\n        \n        for (int i = 0; i < n; i++) {\n            semFoo.wait();\n            printFoo();\n            semBar.notify();\n        }\n    }\n\n    void bar(function<void()> printBar) {\n        \n        for (int i = 0; i < n; i++) {\n            semBar.wait();            \n        \tprintBar();\n            semFoo.notify();\n        }\n    }\n};\n```
['C']