Re: pthread_cond_signal is mutex lock/unlock needed ?



cached byte <jo0x000001a@xxxxxxxxx> writes:
Hi

I want to know if i always need to use the pthread_cond_signal in an
mutex lock / unlock sequence , or can i safely use the
pthread_cond_signal on its own ?

Provided you still change the condition itself under protection of
mutex, like this:

pthread_mutex_lock
change condition
pthread_mutex_unlock
pthread_cond_signal

you are safe. Compared to signalling with mutex locked, it could only
lead to additional spurious wakeups.

On the other hand, the above sequence could be much more efficient than
signalling with mutex locked provided pthread implementation doesn't
implement so called "wait morphing", especially if you use
pthread_cond_broadcast instead of pthread_cond_signal.

-- Sergei.
.



Relevant Pages

  • Re: pthread_cond_signal is mutex lock/unlock needed ?
    ... Compared to signalling with mutex locked, ... Spurious wakeups are harmless, missed wakeups are not. ...
    (comp.os.linux.development.apps)
  • Re: pthread_cond_signal is mutex lock/unlock needed ?
    ... mutex lock / unlock sequence, or can i safely use the ... another thread has acquired the mutex and before it would notice the ... signaling the condition. ...
    (comp.os.linux.development.apps)
  • Re: [newbie] conditions: signal sent before waiter available?
    ... > With a condition variable is a mutex associated. ... > one waiting needs to lock the mutex before calling the wait function, ... use condition variables. ... the signalling ...
    (comp.programming.threads)
  • Re: The condition variable test
    ... Replace the wait operation with a mutex unlock/lock pair. ... Now can you tell me what the thread is waiting for at line 8? ... It is there to optimise the busy waiting loop ... for waiting loop optimisation, e.g. for event signalling. ...
    (comp.programming.threads)
  • Re: Pthread programming: about the lost condition signals.
    ... condition, you MUST, ALWAYS, broadcast. ... mutex without checking that it doesn't leave a situation where you do ... use a single condition variable for more than one predicate without ... which it carefully avoids by signalling while holding the ...
    (comp.programming.threads)