C++ Mutex, Condition Variable Simple code
Note\n\n We don\'t need any wait for printing 1. Hence, no condition variable wait\n We don\'t need to notify anyone after printing 3. Hence, no notify after pr
# Note\n\n* We don\'t need any wait for printing 1. Hence, no condition variable wait\n* We don\'t need to notify anyone after printing 3. Hence, no notify after printing 3.\n* My code doesn\'t work when I do \n`if(currentPrint!=2) cv.wait(lock)`\nbut works only when I do \n`cv.wait(lock,[&](){return currentPrint==2;});`\n\nIt is best to pass the boolean expression as part of wait. Otherwise, there is a timeout.\n\n\n# Code\n```\nclass Foo {\n    condition_variable cv;\n    mutex mtx;\n    int currentPrint;\npublic:\n    Foo() {\n        currentPrint=1;\n    }\n\n    void first(function<void()> printFirst) {\n        unique_lock<mutex>lock(mtx);\n        currentPrint=2;\n        printFirst();\n        cv.notify_all();\n    }\n\n    void second(function<void()> printSecond) {\n        unique_lock<mutex>lock(mtx);\n        cv.wait(lock,[&](){return currentPrint==2;});\n        currentPrint=3;\n        printSecond();\n        cv.notify_one();\n    }\n\n    void third(function<void()> printThird) {\n        unique_lock<mutex>lock(mtx);\n        cv.wait(lock,[&](){return currentPrint==3;});\n        printThird();\n    }\n};\n```\n\n\nPlease let me know if there are any feedbacks on this. Happy coding (^-^)\n
['C++']