Gentoo Archives: gentoo-user

From: "Andrés Becerra Sandoval" <andres.becerra@×××××.com>
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] OT: pthreads condition variable/mutex question
Date: Wed, 13 Aug 2014 17:40:50
Message-Id: CACSNh6M4UAun77gC_7HkSDDs6v9_hGmjRPi=t-j951tvtQUx+w@mail.gmail.com
In Reply to: Re: [gentoo-user] OT: pthreads condition variable/mutex question by "Andrés Becerra Sandoval"
1 2014-08-13 12:36 GMT-05:00 Andrés Becerra Sandoval <andres.becerra@×××××.com>:
2 > 2014-08-13 12:21 GMT-05:00 Grant Edwards <grant.b.edwards@×××××.com>:
3 >> This is not Gentoo specific, and while I'm doing my prototyping and
4 >> development on a Gentoo system, the eventual target is not going to be
5 >> running Gentoo -- so feel free to ignore this thread or throw things
6 >> at me.
7 >>
8 >> I'm trying to figure out how to synchronize threads which may be in
9 >> different processes. Basically, I want thread A to be able to wake up
10 >> any number of other threads B, C, D, ... who are all blocking until A
11 >> says "go" (and who may or may not be in other processes).
12 >>
13 >> Other (mostly embedded) OSes I've used had some sort of "event flag"
14 >> API that did exactly what I'm looking for, but I can't seem to find
15 >> such a thing in pthreads.
16 >>
17 >> A condition variable in shared memory is the closest thing I have
18 >> found, and my test applications are working OK (so far). But, I'm
19 >> unclear on the purpose of the mutex whose address you pass to
20 >> pthread_cond_wait().
21 >>
22 >> Is it to prevent race conditions when manipulating the condition
23 >> variable's internal state? I don't see how that can be the case, since
24 >> the signal/broadcast call would have to be aware of it and it isn't.
25 >>
26 >> The mutex appears to be there to serialize access to some user-defined
27 >> variable(s) (outside the condition variable itself) which I don't
28 >> have. So all the mutex locking/unlocking and resultant blocking of B,
29 >> C, D is just wasted overhead and pointless latency.
30 >>
31 >> pthread_cond_wait(3) says
32 >>
33 >> When using condition variables there is always a Boolean predicate
34 >> involving shared variables associated with each condition wait that
35 >> is true if the thread should proceed. Spurious wakeups from the
36 >> pthread_cond_timedwait() or pthread_cond_wait() functions may
37 >> occur. Since the return from pthread_cond_timedwait() or
38 >> pthread_cond_wait() does not imply anything about the value of this
39 >> predicate, the predicate should be re-evaluated upon such return.
40 >>
41 >> I have no "Boolean predicate" (which presumably comprises the
42 >> "user-defined variables outside the condition variable" I mentioned
43 >> above), and I don't want spurious wakeups, so a pthreads condition
44 >> variable would appear to be the wrong thing to use.
45 >>
46 >> Is there something like an "event flag" similar to a condition
47 >> variable without spurious wakeup problem and without the extra
48 >> overhead of the mutex and "Boolean predicate".
49 >>
50 >> Or am I expected to build my own "event flag" using the aforesaid
51 >> "boolean predicate" just to avoid the spurious wakeup problem? [I'm
52 >> guessing this is the case...]
53 >>
54 >> --
55 >> Grant Edwards grant.b.edwards Yow! I'm DESPONDENT ... I
56 >> at hope there's something
57 >> gmail.com DEEP-FRIED under this
58 >> miniature DOMED STADIUM ...
59 >>
60 >>
61 >
62 > Hi Grant,
63 >
64 > The best explanation I have read is this chapter:
65 > http://pages.cs.wisc.edu/~remzi/OSTEP/threads-cv.pdf
66 >
67 > from the book:
68 >
69 > http://pages.cs.wisc.edu/~remzi/OSTEP/
70 >
71 > I know its 17 pages, but it is worth it!
72 >
73 >
74 > --
75 > Andrés Becerra Sandoval
76
77 In short:
78
79 Withouth the use of the lock, the condition variable and a shared
80 variable in concert you can get in trouble!
81
82
83 --
84 Andrés Becerra Sandoval

Replies

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