C++ Solution
Runtime: 48 ms, faster than 70.67% of C++ online submissions for Print Zero Even Odd.\nMemory Usage: 9 MB, less than 100.00% of C++ online submissions for Print
Runtime: 48 ms, faster than 70.67% of C++ online submissions for Print Zero Even Odd.\nMemory Usage: 9 MB, less than 100.00% of C++ online submissions for Print Zero Even Odd.\n\n```\nclass ZeroEvenOdd {\nprivate:\n    int n;\n    mutex m;\n    condition_variable cv;\n    bool print_zero = true;\n    int int_to_print = 1;\npublic:\n    ZeroEvenOdd(int n) {\n        this->n = n;\n    }\n\n    // printNumber(x) outputs "x", where x is an integer.\n    void zero(function<void(int)> printNumber) {\n        while (int_to_print <= n) {\n            unique_lock<mutex> lk(m);\n            cv.wait(lk, [this]{return print_zero;});\n            if (int_to_print <= n)\n                printNumber(0);\n            print_zero = false;\n            lk.unlock();\n            cv.notify_all();\n        }\n        return ;\n    }\n\n    void even(function<void(int)> printNumber) {\n        while (int_to_print <= n) {\n            unique_lock<mutex> lk(m);\n            cv.wait(lk, [this]{return !print_zero && int_to_print % 2 == 0;});\n            if (int_to_print <= n) {\n                printNumber(int_to_print);\n                ++int_to_print;\n                print_zero = true;\n            }\n            lk.unlock();\n            cv.notify_all();\n        }\n        return ;\n    }\n\n    void odd(function<void(int)> printNumber) {\n        while (int_to_print <= n) {\n            unique_lock<mutex> lk(m);\n            cv.wait(lk, [this]{return !print_zero && int_to_print % 2 != 0;});\n            if (int_to_print <= n) {\n                printNumber(int_to_print);\n                ++int_to_print;\n                print_zero = true;\n            }\n            lk.unlock();\n            cv.notify_all();\n        }\n        return ;\n    }\n};\n```
[]