If a thread calls pthread_cond_wait(cond_ptr,mutex_ptr) will a null cond_ptr, is it guaranteed to not fall asleep?
According to http://pubs.opengroup.org/onlinepubs/007908799/xsh/pthread_cond_wait.html, a null cond_ptr just means pthread_cond_wait() may (not an emphatic will) fail, so I guess threads can then fall asleep on null condition variables?
I can't see a valid use case for this and I'm wondering why it would ever matter. You shouldn't be calling
pthread_cond_waitwith an invalid condition variable.If you're worried about it, just change your code from:
to something like:
and it won't be called with a NULL.
I suspect it was put in as "may" simply because there was an implementation of pthreads (perhaps even the original DEC threads itself) which didn't return a failure code for that circumstance.
But, since the alternative is almost certainly that the whole thing fell in a screaming heap, I wouldn't be relying on it :-)
If you're worried about the atomicity of that code, you needn't be. Simply use the same mutex that protects the condition variable to protect the CV pointer being held in your list:
and, in your looping code:
The fact that the mutex is locked during both the
ifand callingpthread_condvar_waitmeans that no race condition can exist. Nothing can change the node condition variables until the looping thread releases the mutex within thepthread_condvar_waitcall. And by that time, the call is using its own local copy of the pointer so changing te one in the list will have no effect.And, if the node-changing code grabs the mutex, the
ifandpthread_condvar_waitcan't execute until that mutex is released.