I've been thinking about the logic behind condition variable for a while, and getting comfortable with most common questions associated with it.
Basically, if We do something like:
mutex.lock()
while(!predicate()){
mutex.unlock();
pthread_cond_wait(&cv); // with mutex decoupled, no mutex is passed in I guess.
mutex.lock();
}
mutex.unlock();
The window between unlock() and wait() is something we need to eliminate, and that's pretty much why cv exists in the first place.
Then I found this:
Features of Mutexes and Condition Variables
It had been suggested that the mutex acquisition and release be decoupled from condition wait. This was rejected because it is the combined nature of the operation that, in fact, facilitates realtime implementations. Those implementations can atomically move a high-priority thread between the condition variable and the mutex in a manner that is transparent to the caller. This can prevent extra context switches and provide more deterministic acquisition of a mutex when the waiting thread is signaled. Thus, fairness and priority issues can be dealt with directly by the scheduling discipline. Furthermore, the current condition wait operation matches existing practice.
(https://linux.die.net/man/3/pthread_cond_wait)
I just don't understand how pthread_cond_wait() can be decoupled with the mutex usage.
Hope someone can show me how can it be achieved, Or: Do I misunderstand the meaning of "decouple" in the comment?