Using Conditional Variables and lock :: std:: C++ Easy sol
\n\n# Code\n\nclass Foo {\n    std:: mutex m;\n    std:: condition_variable cv;\n    int turn;\npublic:\n    Foo() {\n        turn= 0;\n    }\n\n    void first(
\n\n# Code\n```\nclass Foo {\n    std:: mutex m;\n    std:: condition_variable cv;\n    int turn;\npublic:\n    Foo() {\n        turn= 0;\n    }\n\n    void first(function<void()> printFirst) {\n        \n        // printFirst() outputs "first". Do not change or remove this line.\n        printFirst();\n                turn=1;\n        cv.notify_all();\n    }\n\n    void second(function<void()> printSecond) {\n                std:unique_lock<std::mutex>lock(m);\n        while(turn!=1){\n            cv.wait(lock);\n        }\n        // printSecond() outputs "second". Do not change or remove this line.\n        printSecond();\n                turn=2;\n        cv.notify_all();\n\n    }\n\n    void third(function<void()> printThird) {\n                        std:unique_lock<std::mutex>lock(m);\n        while(turn!=2){\n            cv.wait(lock);\n        }\n        // printThird() outputs "third". Do not change or remove this line.\n        printThird();\n\n    }\n};\n```
['C++']