When is a condition variable needed, isn't a mutex enough?

前端 未结 6 1217
忘掉有多难
忘掉有多难 2020-11-30 19:06

I\'m sure mutex isn\'t enough that\'s the reason the concept of condition variables exist; but it beats me and I\'m not able to convince myself with a concrete scenario when

6条回答
  •  粉色の甜心
    2020-11-30 19:41

    I think it is implementation defined.
    The mutex is enough or not depends on whether you regard the mutex as a mechanism for critical sections or something more.

    As mentioned in http://en.cppreference.com/w/cpp/thread/mutex/unlock,

    The mutex must be locked by the current thread of execution, otherwise, the behavior is undefined.

    which means a thread could only unlock a mutex which was locked/owned by itself in C++.
    But in other programming languages, you might be able to share a mutex between processes.

    So distinguishing the two concepts may be just performance considerations, a complex ownership identification or inter-process sharing are not worthy for simple applications.


    For example, you may fix @slowjelj's case with an additional mutex (it might be an incorrect fix):

    Thread1:

    lock(mutex0);
    while(1) {
        lock(mutex0); // Blocks waiting for notification from Thread2
        ... // do work after notification is received
        unlock(mutex1); // Tells Thread2 we are done
    }
    

    Thread2:

    while(1) {
        lock(mutex1); // lock the mutex so Thread1 will block again
        ... // do the work that precedes notification
        unlock(mutex0); // unblocks Thread1
    }
    

    But your program will complain that you have triggered an assertion left by the compiler (e.g. "unlock of unowned mutex" in Visual Studio 2015).

提交回复
热议问题