[C++]Why most of the solutions using mutex are wrong+solution
I actually started learning about threads recently only and even my first attempt was to take 2 mutex and lock/unlock them in order desired by us. That solution
I actually started learning about threads recently only and even my first attempt was to take 2 mutex and lock/unlock them in order desired by us. That solution is actually accepted in leetcode.\n\nHowever, while researching for difference between mutex and conditional_variable usage, i realised that the way mutex are being used here are totally wrong.\n\nSome points that must be taken note of are: \n* **Mutex** are used for **mutual exclusion** i.e to safe gaurd the critical sections of a code.\n* **Semaphone/condition_variable** are used for **thread synchronisation**(which is what we want to achieve here).\n* **Mutex have ownership assigned with them**, that is to say, *the thread that locks a mutex must only unlock it.* Also, we must not unlock a mutex that has not been locked **(This is what most programs have got wrong)**.\n* If the mutex is not used as said above, **the behavior is undefined**, which however in our case produces the required result.\n\nReferences:\n1. [If the mutex is not currently locked by the calling thread, it causes undefined behavior.](http://www.cplusplus.com/reference/mutex/mutex/unlock/)\n2. [The precondition for calling unlock is holding an ownership of the mutex, according to (std)30.4.1.2](https://stackoverflow.com/questions/43487357/how-stdmutex-got-unlocked-in-different-thread)\n\nI did read few more places the same thing, but i think these do put the point across :)\n\nNow my solution using a condition_variable:\n\n```\nclass Foo {\npublic:\n    int count = 0;\n    mutex mtx;\n    condition_variable cv;\n    Foo() {\n        count = 1;\n        //cv.notify_all();\n    }\n\n    void first(function<void()> printFirst) {\n        \n        unique_lock<mutex> lck(mtx);\n\t\t// No point of this wait as on start count will be 1, we need to make the other threads wait.\n        // while(count != 1){\n        //     cv.wait(lck);\n        // }\n        // printFirst() outputs "first". Do not change or remove this line.\n\n        printFirst();\n        count = 2;\n        cv.notify_all();\n    }\n\n    void second(function<void()> printSecond) {\n        unique_lock<mutex> lck(mtx);\n        while(count != 2){\n            cv.wait(lck);\n        }\n        // printSecond() outputs "second". Do not change or remove this line.\n        printSecond();\n        count = 3;\n        cv.notify_all();\n    }\n\n    void third(function<void()> printThird) {\n        unique_lock<mutex> lck(mtx);\n        while(count != 3){\n            cv.wait(lck);\n        }\n        // printThird() outputs "third". Do not change or remove this line.\n        printThird();\n    }\n};\n```\n\n**Disclaimer**:  I am still learning :P. So, please do enlighten me if you think my understanding of this topic is wrong/misleading.\nThanks for reading :)
['C']