Re: pthread_cond_signal is mutex lock/unlock needed ?



David Schwartz <davids@xxxxxxxxxxxxx> writes:

On Feb 14, 9:47 am, Sergei Organov <o...@xxxxxxxxx> wrote:

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.

This is incorrect. You are not necessarily safe. The problem is that
the 'pthread_cond_signal' might signal a thread that blocked after the
'pthread_mutex_unlock' call rather than one that was already blocked.

Yeah, me stands corrected. I was not careful enough choosing words. What
I actually meant to say is that POSIX doesn't require mutex to be locked
in order to safely call pthread_cond_signal(). It's indeed not
necessarily safe from the POV of particular application logic. Though to
be picky, I can argue that calling pthread_cond_signal() with mutex
locked is not necessarily safe either ;)

-- Sergei.
.



Relevant Pages

  • Re: Issues with multiple instances
    ... definitely a safe workaround. ... I have implemented a named mutex as suggested in one of the earlier ... iSplashForm = new SABannerForm; ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: [PATCH] Fix some kallsyms_lookup() vs rmmod races
    ... inform it if it is safe to take a mutex or not. ... This patch is techically dependent on http://lkml.org/lkml/2007/3/14/128 aka ...
    (Linux-Kernel)
  • Re: [PATCH] Fix some kallsyms_lookup() vs rmmod races
    ... module_address_lookupsays it's for oops resolution so races are ... inform it if it is safe to take a mutex or not. ... it just creates new dependencies (hint: this patch shouldn't even compile with current git since module_mutex is not declared in module.h, not to mention compile when CONFIG_MODULES not set) ...
    (Linux-Kernel)
  • Re: Writing Singleton Classes
    ... > Singletons are never thread safe. ... Mutex means Mutual Exclusion (it's a mechanism that ensures ... threads from executing the sequence until Unlock() has been ...
    (comp.lang.cpp)
  • 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)