Current-Users archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

pthread_cond_signal/broadcast: necessary to hold mutex?



Hi!

The NetBSD man page for pthread_cond_broadcast/signal claims that you
need to hold the mutex when sending the signal:

(about pthread_cond_*wait)

     With both functions the waiting thread unblocks after another thread
     calls pthread_cond_signal() or pthread_cond_broadcast() with the same
     condition variable and by holding the same mutex that was associated with
     cond by either one of the blocking functions.

...

(about pthread_cond_broadcast/signal)
     The same mutex must be held while
     broadcasting or signaling on cond.

but later also:

     In this implementation, none of the functions enforce this
     requirement, but if the mutex is not held or independent mutexes
     are used the resulting behaviour is undefined.

On the other hand, POSIX says you don't need to hold the mutex when
signaling:

https://pubs.opengroup.org/onlinepubs/9799919799/functions/pthread_cond_broadcast.html

    The pthread_cond_broadcast() or pthread_cond_signal() functions
    may be called by a thread whether or not it currently owns the
    mutex that threads calling pthread_cond_clockwait(),
    pthread_cond_timedwait(), or pthread_cond_wait() have associated
    with the condition variable during their waits; however, if
    predictable scheduling behavior is required, then that mutex shall
    be locked by the thread calling pthread_cond_broadcast() or
    pthread_cond_signal().

Holding the mutex wasn't even required in the POSIX '97 edition:
https://pubs.opengroup.org/onlinepubs/007908799/xsh/pthread_cond_signal.html

     The pthread_cond_signal() or pthread_cond_broadcast() functions
     may be called by a thread whether or not it currently owns the
     mutex that threads calling pthread_cond_wait() or
     pthread_cond_timedwait() have associated with the condition
     variable during their waits; however, if predictable scheduling
     behaviour is required, then that mutex is locked by the thread
     calling pthread_cond_signal() or pthread_cond_broadcast().

- Time for a bug report?

- Any ideas where this requirement comes from?

Cheers,
 Thomas


Home | Main Index | Thread Index | Old Index