Question

Does a condition variable have a number - the number of times it has been signaled associated with it (like a semaphore)? I.e., if five threads signal the condition variable, and assuming all of them are canceled, will it be possible for the condition variable to be waited on by five other threads, afterwards? Or, will only one thread unblock after calling wait?

Was it helpful?

Solution

if five threads signal the condition variable, and assuming all of them are canceled, will it be possible for the condition variable to be waited on by five other threads, afterwards?

Although this is very language and implementation specific, typically the answer is that there is not a number. If a condition is signalled without there being any threads waiting on the condition then the signals are lost. If 5 signals are generated and then a thread waits on the condition, it has to wait for the next signal to be delivered.

Now that the question is about C (I assume POSIX threads) then I can add details from the pthread_cond_signal() manual:

The pthread_cond_broadcast() and pthread_cond_signal() functions shall have no effect if there are no threads currently blocked on cond.

There is no counter that keeps track of POSIX condition signals.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top