C++ lock-free, no mutexes at all
class ZeroEvenOdd {\nprivate:\n    int n;\n    atomic lastPrinted; \n    const int kHalfSize = 16;\n    const unsigned int kInitialValue;\n    \npublic:\n    Ze
class ZeroEvenOdd {\nprivate:\n    int n;\n    atomic<unsigned int> lastPrinted; \n    const int kHalfSize = 16;\n    const unsigned int kInitialValue;\n    \npublic:\n    ZeroEvenOdd(int n): kInitialValue(n+1) {\n        this->n = n;\n        lastPrinted = kInitialValue;\n    }\n\n    void printNextAndUpdate(function<void(int)> printNumber, int next)\n    {\n        //cout << next << endl;\n        printNumber(next);\n        \n        if( lastPrinted == kInitialValue)\n        {\n            lastPrinted = 0;\n        }\n        else\n        {\n            int t = lastPrinted << kHalfSize;\n            t += next;\n            lastPrinted = t;\n        }\n    }\n    \n    // printNumber(x) outputs "x", where x is an integer.\n    void zero(function<void(int)> printNumber) {\n        cout << "Started Zero" << endl;\n        int next;\n        while ( (next = getNextToPrint()) >= 0)\n        {\n            if( next == 0)\n            {\n                printNextAndUpdate(printNumber, next);\n            }\n            else\n            {\n                std::this_thread::yield();\n            }\n        }\n\n    }\n\n    void even(function<void(int)> printNumber) {\n        cout << "Started even" << endl;\n        int next;\n        while ((next = getNextToPrint() ) >= 0)\n        {\n            if( next > 0 && next % 2 == 0)\n            {\n                printNextAndUpdate(printNumber, next);\n            }\n            else\n            {\n                std::this_thread::yield();\n            }\n        }\n\n    }\n\n    void odd(function<void(int)> printNumber) {\n        cout << "Started odd" << endl;\n        int next;\n        while ((next = getNextToPrint() ) >= 0)\n        {\n            if( next % 2 == 1)\n            {\n                printNextAndUpdate(printNumber, next);\n            }\n            else\n            {\n                std::this_thread::yield();\n            }\n        }\n    }\n    \n    int getNextToPrint()\n    {\n        if (lastPrinted == kInitialValue) return 0;\n        \n        unsigned int localLastPrinted = lastPrinted;\n        int last = ( localLastPrinted << kHalfSize ) >> kHalfSize;\n        int beforeLast = localLastPrinted >> kHalfSize;\n    \n        if( last == n ) return -1;\n        if( last != 0 ) return 0;\n        return beforeLast + 1;\n    }\n};
[]