Gentoo Archives: gentoo-user

From: Alan McKinnon <alan.mckinnon@×××××.com>
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] OT: pthreads condition variable/mutex question
Date: Wed, 13 Aug 2014 20:12:00
Message-Id: 53EBC651.8050903@gmail.com
In Reply to: [gentoo-user] OT: pthreads condition variable/mutex question by Grant Edwards
1 On 13/08/2014 19:21, Grant Edwards wrote:
2 > This is not Gentoo specific, and while I'm doing my prototyping and
3 > development on a Gentoo system, the eventual target is not going to be
4 > running Gentoo -- so feel free to ignore this thread or throw things
5 > at me.
6 >
7 > I'm trying to figure out how to synchronize threads which may be in
8 > different processes. Basically, I want thread A to be able to wake up
9 > any number of other threads B, C, D, ... who are all blocking until A
10 > says "go" (and who may or may not be in other processes).
11
12
13 Sounds like you a "one talker - many listeners" model. Have you
14 considered a really simple solution like dbus?
15
16
17
18 >
19 > Other (mostly embedded) OSes I've used had some sort of "event flag"
20 > API that did exactly what I'm looking for, but I can't seem to find
21 > such a thing in pthreads.
22 >
23 > A condition variable in shared memory is the closest thing I have
24 > found, and my test applications are working OK (so far). But, I'm
25 > unclear on the purpose of the mutex whose address you pass to
26 > pthread_cond_wait().
27 >
28 > Is it to prevent race conditions when manipulating the condition
29 > variable's internal state? I don't see how that can be the case, since
30 > the signal/broadcast call would have to be aware of it and it isn't.
31 >
32 > The mutex appears to be there to serialize access to some user-defined
33 > variable(s) (outside the condition variable itself) which I don't
34 > have. So all the mutex locking/unlocking and resultant blocking of B,
35 > C, D is just wasted overhead and pointless latency.
36 >
37 > pthread_cond_wait(3) says
38 >
39 > When using condition variables there is always a Boolean predicate
40 > involving shared variables associated with each condition wait that
41 > is true if the thread should proceed. Spurious wakeups from the
42 > pthread_cond_timedwait() or pthread_cond_wait() functions may
43 > occur. Since the return from pthread_cond_timedwait() or
44 > pthread_cond_wait() does not imply anything about the value of this
45 > predicate, the predicate should be re-evaluated upon such return.
46 >
47 > I have no "Boolean predicate" (which presumably comprises the
48 > "user-defined variables outside the condition variable" I mentioned
49 > above), and I don't want spurious wakeups, so a pthreads condition
50 > variable would appear to be the wrong thing to use.
51 >
52 > Is there something like an "event flag" similar to a condition
53 > variable without spurious wakeup problem and without the extra
54 > overhead of the mutex and "Boolean predicate".
55 >
56 > Or am I expected to build my own "event flag" using the aforesaid
57 > "boolean predicate" just to avoid the spurious wakeup problem? [I'm
58 > guessing this is the case...]
59 >
60
61
62 --
63 Alan McKinnon
64 alan.mckinnon@×××××.com

Replies

Subject Author
[gentoo-user] Re: OT: pthreads condition variable/mutex question Grant Edwards <grant.b.edwards@×××××.com>