C++ condition variable clean code easy to understand
C++\nclass Foo {\n    atomic<int> cur;\n    mutex mtx;\n    condition_variable cv;\nprotected:\n    void do_work(function<void()> printS, function<bool()> check
```C++\nclass Foo {\n    atomic<int> cur;\n    mutex mtx;\n    condition_variable cv;\nprotected:\n    void do_work(function<void()> printS, function<bool()> check) {\n        std::unique_lock<mutex> lk(mtx);\n        cv.wait(lk, [&]{return check();});\n        \n        cur = (cur+1)%4;\n        printS();\n        cv.notify_all();\n    }\n    \npublic:\n    Foo() {\n       this->cur = 1;\n    }\n\n    void first(function<void()> printFirst) {\n        // printFirst() outputs "first". Do not change or remove this line.\n        do_work(printFirst, [&]{return cur == 1;});\n    }\n\n    void second(function<void()> printSecond) {\n        // printSecond() outputs "second". Do not change or remove this line.\n        do_work(printSecond, [&]{return cur == 2;});\n    }\n\n    void third(function<void()> printThird) {\n        \n        // printThird() outputs "third". Do not change or remove this line.\n       do_work(printThird, [&]{return cur == 3;});\n    }\n};\n```
['C', 'C++']