They are not exactly mutexes with extra magic, although in some abstractions (monitoring is used as java and C #), the condition variable and the mutex are combined into one block. The purpose of the condition variables is to avoid waiting / polling in standby mode and hinting at the runtime, which should be scheduled "next." Think about how you could write this example without condition variables.
while(1) { lock(mymutex) if( somevalue != 0) break; unlock(mymutex); } if( somevalue == 0xdeadbeef ) myfunc();
You will sit in a tight loop in this thread, burning a lot of processor and making a lot of debate about locking. If locking / unlocking a mutex is cheap enough, you may find yourself in a situation where otherthread never has the ability to get a lock (although real-world mutexes usually distinguish between thread ownership and lock presence, and also have fairness concepts, so this is unlikely to actually happen) .
You can reduce the wait by inserting a dream,
while(1) { lock(mymutex) if( somevalue != 0) break; unlock(mymutex); sleep(1); // let some other thread do work }
but how long is a good time to sleep? Basically you are just wondering. Runtime also cannot understand why you are sleeping, or what you are waiting for. The condition variable allows the runtime to at least to some extent know which threads are currently interested in the same event.
source share