Note that pthread_cond_signal() is generally intended to wake up only one waiting thread (that's all that it guarantees). But it could wake more 'accidentally'. The while (avail > 0) loop performs two functions.
Note that pthread_cond_signal() is generally intended to wake up only one waiting thread (that's all that it guarantees). But it could wake more 'accidentally'. The while (avail > 0) loop performs two functions: it allows the one thread guaranteed to be woken up to consume all queued work units it prevents additional 'accidentally' awakened threads from assuming that there's work to be done, when there might not be since the initial thread would have handled all of them.It also prevents a race condition where a work unit might have been placed on the queue after the while (avail > 0) has completed, but before the worker thread has waited on the condition again - but that race is also handled by the if test just before calling pthread_cond_wait().
Basically when a thread is awakened, all it knows is that there might be work units for it to consume, but there might not (another thread might have consumed them). So the sequence of events that occurs when pthread_cond_signal() is called is: the system will wake one or more threads waiting on the condition all the threads that are awakened will then try to acquire the mutex - only one of them can acquire it at any particular moment, since that's the purpose of a mutex that thread will then proceed, perform the work in the while (avail > 0) loop, then will release the mutex at that point one of the other threads that were previously woken up will acquire the mutex and work the same loop, then release the mutex. Generally, there will be no work units available anymore (since the first thread would have consumed all of them), but if another thread had added an additional unit (or more), then this thread would handle that work the next thread will acquire the mutex and perform that same set of logic.
Thanks for the insightful answer. Maybe I haven't expressed it clearly but I wanted to know what, after the wake up call, is the thread that lost contest for the mutex doing now? I assume it has to stop running the code because the mutex has been claimed by another thread (winner) and we're in the critical section, BUT the question is:** if the loosing thread returned from pthread_cond_wait() where is it "stuck" now?
** Does it somehow, automagically return to the pthread_cond_wait() (@Nikolai)? @Hasturkun almost answered my question. – racic Jun 30 at 6:56 ...continued: I'm guessing, because pthread_cond_wait() on exit atomically aquires the mutex (winner case), the loosing thread will stop immediately (before checking again for while(avail==0) condition) after it returned from its pthread_cond_wait().
Am I correct? – racic Jun 30 at 7:07 2 The 'losing' thread will be blocked on the mutex before returning from pthread_cond_wait() since pthread_cond_wait() must acquire the mutex for the thread before letting it return. Once the 'winning' thread releases the mutex, the losing thread will be able to acquire it and proceed to return from pthread_cond_wait().
– Michael Burr Jun 30 at 14:06 Ok I think it's clear now, thanks. – racic Jun 30 at 14:59.
Pthread_cond_wait() has to acquire given mutex once signaled/woken up. If another thread wins that race, the function blocks until the mutex is released. So from the application point of view it doesn't return until current thread holds the mutex.
The wait is always done in a loop (while (avail == 0) { ... above) to make sure that application condition we are waiting for still holds (buffer not empty, more work available, etc. ) Hope this helps.
The thread that lost the contest wakes up once the mutex is unlocked, checks the condition again, then goes to sleep on the condition variable.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.