Why pthread cond wait need mutex




















Prerequisite : Multithreading We provide nothing but the best curated videos and practice problems for our students. Check out the C Foundation Course and master the C language from basic to advanced level. Wait no more, start learning today! Return Value : On success, 0 is returned ; otherwise, an error number shall be returned to indicate the error. Note : The above two functions works together. Below is the implementation of condition, wait and signal functions.

Skip to content. Change Language. Related Articles. This library is usually included automatically. The calling thread must have locked mutex before waiting on the condition variable.

On return from the function, the mutex is again locked and owned by the calling thread. This practice is almost automatically enforced by the desire of programmers to avoid long serial regions of execution which would reduce total effective parallelism. When using mutexes and condition variables, one tries to ensure that the usual case is to lock the mutex, access shared data, and unlock the mutex.

Waiting on a condition variable should be a relatively rare situation. For example, when implementing a read-write lock, code that acquires a read-lock typically needs only to increment the count of readers under mutual-exclusion and return.

The calling thread would actually wait on the condition variable only when there is already an active writer. Note that in the usual case there is no context switch. This is not to say that the efficiency of condition waiting is unimportant. Since there needs to be at least one context switch per Ada rendezvous, the efficiency of waiting on a condition variable is important.

The cost of waiting on a condition variable should be little more than the minimal cost for a context switch plus the time to unlock and lock the mutex. 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. Synchronization primitives that attempt to interfere with scheduling policy by specifying an ordering rule are considered undesirable. Threads waiting on mutexes and condition variables are selected to proceed in an order dependent upon the scheduling policy rather than in some fixed order for example, FIFO or priority.

Thus, the scheduling policy determines which thread s are awakened and allowed to proceed.



0コメント

  • 1000 / 1000