c++ use mutex
Intuition\nUse condition variable and count variable and based on the count value , the desigenated thread will be running accordingly \n\n\n\nclass Foo {\npubl
# Intuition\nUse condition variable and count variable and based on the count value , the desigenated thread will be running accordingly \n\n\n```\nclass Foo {\npublic:\n\n    std::mutex m; \n    std::condition_variable cv;\n    int count; \n \n    Foo():count{0} {\n        \n    }\n\n    void first(function<void()> printFirst) {\n        std::unique_lock<std::mutex> lock(m);\n\n        count++;\n        // printFirst() outputs "first". Do not change or remove this line.\n        printFirst();\n        lock.unlock(); \n        cv.notify_all();\n    }\n\n    void second(function<void()> printSecond) {\n        std::unique_lock<std::mutex> lock(m);\n        cv.wait(lock , [this](){return count == 1 ? true : false;});\n        // printSecond() outputs "second". Do not change or remove this line.\n        printSecond();\n        count++;\n        lock.unlock(); \n        cv.notify_all();\n\n    }\n\n    void third(function<void()> printThird) {\n        std::unique_lock<std::mutex> lock(m);\n        cv.wait(lock , [this](){return count == 2 ? true : false;});\n        // printThird() outputs "third". Do not change or remove this line.\n        printThird();\n        lock.unlock(); \n        cv.notify_all();\n    }\n};\n```
['C++']