public inbox for gentoo-user@lists.gentoo.org
 help / color / mirror / Atom feed
From: "Andrés Becerra Sandoval" <andres.becerra@gmail.com>
To: gentoo-user@lists.gentoo.org
Subject: Re: [gentoo-user] OT: pthreads condition variable/mutex question
Date: Wed, 13 Aug 2014 12:40:42 -0500	[thread overview]
Message-ID: <CACSNh6M4UAun77gC_7HkSDDs6v9_hGmjRPi=t-j951tvtQUx+w@mail.gmail.com> (raw)
In-Reply-To: <CACSNh6Mu+_RGjywVxQi-e_HGmeMBCwoiQ=0t7gBNo-Q0pHWc_w@mail.gmail.com>

2014-08-13 12:36 GMT-05:00 Andrés Becerra Sandoval <andres.becerra@gmail.com>:
> 2014-08-13 12:21 GMT-05:00 Grant Edwards <grant.b.edwards@gmail.com>:
>> This is not Gentoo specific, and while I'm doing my prototyping and
>> development on a Gentoo system, the eventual target is not going to be
>> running Gentoo -- so feel free to ignore this thread or throw things
>> at me.
>>
>> I'm trying to figure out how to synchronize threads which may be in
>> different processes.  Basically, I want thread A to be able to wake up
>> any number of other threads B, C, D, ... who are all blocking until A
>> says "go" (and who may or may not be in other processes).
>>
>> Other (mostly embedded) OSes I've used had some sort of "event flag"
>> API that did exactly what I'm looking for, but I can't seem to find
>> such a thing in pthreads.
>>
>> A condition variable in shared memory is the closest thing I have
>> found, and my test applications are working OK (so far).  But, I'm
>> unclear on the purpose of the mutex whose address you pass to
>> pthread_cond_wait().
>>
>> Is it to prevent race conditions when manipulating the condition
>> variable's internal state? I don't see how that can be the case, since
>> the signal/broadcast call would have to be aware of it and it isn't.
>>
>> The mutex appears to be there to serialize access to some user-defined
>> variable(s) (outside the condition variable itself) which I don't
>> have. So all the mutex locking/unlocking and resultant blocking of B,
>> C, D is just wasted overhead and pointless latency.
>>
>> pthread_cond_wait(3) says
>>
>>    When using condition variables there is always a Boolean predicate
>>    involving shared variables associated with each condition wait that
>>    is true if the thread should proceed. Spurious wakeups from the
>>    pthread_cond_timedwait() or pthread_cond_wait() functions may
>>    occur. Since the return from pthread_cond_timedwait() or
>>    pthread_cond_wait() does not imply anything about the value of this
>>    predicate, the predicate should be re-evaluated upon such return.
>>
>> I have no "Boolean predicate" (which presumably comprises the
>> "user-defined variables outside the condition variable" I mentioned
>> above), and I don't want spurious wakeups, so a pthreads condition
>> variable would appear to be the wrong thing to use.
>>
>> Is there something like an "event flag" similar to a condition
>> variable without spurious wakeup problem and without the extra
>> overhead of the mutex and "Boolean predicate".
>>
>> Or am I expected to build my own "event flag" using the aforesaid
>> "boolean predicate" just to avoid the spurious wakeup problem?  [I'm
>> guessing this is the case...]
>>
>> --
>> Grant Edwards               grant.b.edwards        Yow! I'm DESPONDENT ... I
>>                                   at               hope there's something
>>                               gmail.com            DEEP-FRIED under this
>>                                                    miniature DOMED STADIUM ...
>>
>>
>
> Hi Grant,
>
> The best explanation I have read is this chapter:
> http://pages.cs.wisc.edu/~remzi/OSTEP/threads-cv.pdf
>
> from the book:
>
> http://pages.cs.wisc.edu/~remzi/OSTEP/
>
> I know its 17 pages, but it is worth it!
>
>
> --
>   Andrés Becerra Sandoval

In short:

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


-- 
  Andrés Becerra Sandoval


  reply	other threads:[~2014-08-13 17:40 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-08-13 17:21 [gentoo-user] OT: pthreads condition variable/mutex question Grant Edwards
2014-08-13 17:36 ` Andrés Becerra Sandoval
2014-08-13 17:40   ` Andrés Becerra Sandoval [this message]
2014-08-13 18:05     ` Alec Ten Harmsel
2014-08-13 19:23       ` [gentoo-user] " Grant Edwards
2014-08-13 19:57         ` Alec Ten Harmsel
2014-08-13 20:19           ` Grant Edwards
2014-08-13 20:50             ` Alec Ten Harmsel
2014-08-22  8:39             ` J. Roeleveld
2014-08-13 19:41     ` Grant Edwards
2014-08-13 20:10 ` [gentoo-user] " Alan McKinnon
2014-08-13 20:28   ` [gentoo-user] " Grant Edwards
2014-08-13 20:32     ` Alan McKinnon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CACSNh6M4UAun77gC_7HkSDDs6v9_hGmjRPi=t-j951tvtQUx+w@mail.gmail.com' \
    --to=andres.becerra@gmail.com \
    --cc=gentoo-user@lists.gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox