C++ using Mutex and Condition variable [97% faster]
Code is self explanatory :)\n\nclass ZeroEvenOdd {\nprivate:\n    int n;\n    std::mutex mtx;\n    std::condition_variable cv;\n    std::atomic<int>counter=1;\n
Code is self explanatory :)\n```\nclass ZeroEvenOdd {\nprivate:\n    int n;\n    std::mutex mtx;\n    std::condition_variable cv;\n    std::atomic<int>counter=1;\n    std::atomic<bool> zeroTurn = true;\n\npublic:\n    ZeroEvenOdd(int n) \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    {\n        std::unique_lock <std::mutex> lk(mtx);\n        while(true)\n        {\n            cv.wait(lk,[this](){ return (counter>n || zeroTurn ); });\n            if(counter>n)\n                return;\n            printNumber(0);\n            zeroTurn= false;\n            cv.notify_all();\n        }\n    }\n\n    void even(function<void(int)> printNumber) \n    {\n        std::unique_lock <std::mutex> lk(mtx);\n        while(true)\n        {\n            cv.wait(lk,[this](){ return (counter>n || (counter % 2 == 0 && !zeroTurn) ); });\n            if(counter>n)\n                return;\n            printNumber(counter);\n            zeroTurn= true;\n            ++counter;\n            cv.notify_all();\n        }\n    }\n\n    void odd(function<void(int)> printNumber) \n    {\n        std::unique_lock <std::mutex> lk(mtx);\n        while(true)\n        {\n            cv.wait(lk,[this](){ return (counter>n || (counter % 2 == 1 && !zeroTurn) ); });\n            if(counter>n)\n                return;\n            printNumber(counter);\n            zeroTurn= true;\n            ++counter;\n            cv.notify_all();\n        }\n    }\n};\n```
['C++']