On 2014-08-13, Andrés Becerra Sandoval <[email protected]> wrote:

> In short:
>
> Withouth the use of the lock, the condition variable and a shared
> variable in concert you can get in trouble!

That is often true, but 

 1) I don't have a shared variable that I want to associate with the
    condition variable.  There is some shared data, but I'm going to
    use means other than a mutex to control access to it.
    
 2) In most of the examples presented in that article the shared
    variable is an int that is set to 0 or 1 by one thread and is
    tested for non-zero value by the other.  I'm not aware of any CPU
    on which Linux runs where those operations can result in a race
    condition thus requiring a mutex.  [Yes I know that isn't
    guaranteed by the C language definiton, but one can verify that
    it's true.]

If I create a shared mutex just to make pthread_cond_wait happy, and
one of the "clients" (the ones calling pthread_cond_wait) dies while
holding the mutex, then it deadlocks all of the other clients.  To be
safe it seems one should create a private mutex for each thread that's
going to call pthread_cond_wait().  Private mutexes should be very
little overhead in the uncontended case, so that's an option.
    
-- 
Grant Edwards               grant.b.edwards        Yow! I wonder if I should
                                  at               put myself in ESCROW!!
                              gmail.com            


Reply via email to