Re: Pthread programming: about the lost condition signals.
- From: David Schwartz <davids@xxxxxxxxxxxxx>
- Date: Tue, 3 Jun 2008 19:38:34 -0700 (PDT)
Szabolcs Ferenczi wrote:
I am afraid your example has some logic and concurrency bugs whether
you use method one or method two.
You are correct. My example contains several defects. However, the
principle is correct.
One concurrency bug is that you must give a signal whenever you update
the shared variable and you miss to give any signal in your example.
No, you don't have to. The type A and type B threads resolve something
that is made to need resolution by other threads. They do not create
any situation that needs resolution by another thread.
If a thread makes the predicate true and it's supposed to be false, it
signals to get the appropriate A or B thread to fix the problem. If a
thread makes the predicate false and it's supposed to be true, it
signals to get the appropriate A or B thread to fix the problem. Once
the A or B thread runs, the job is done, and the predicate is as the
logic needs it.
Only the threads that "break" the predicate need to signal.
The third bug and this is the main one: If you mean to use one and the
same condition variable for the two different predicates, I think you
do, it is possible that the set of threads waiting on the same cv
contain both thread A and thread B types. Consequently, a signal on
the same cv may wake up the `wrong' thread whether method one or two
is used.
Yeah, you are correct. I can fix it by having the signalling thread
create the only type B thread while it holds the mutex. There are
better ways to fix it, and I've got a fixed example somewhere on
USENET. I failed in my attempt to hastily reconstruct it though.
The point stands though, there is a huge semantic difference when you
move pthread_cond_signal outside of the lock -- in one case you cannot
signal a thread that chose to block on the basis of the changed
predicate and in the other case you can.
As I showed though, it's hard to create a correct case where this
matters even when you're trying to do so. So the odds of encountering
one in the wild are basically zero.
The solution is either to use two distinct cv for the two predicates
or use broadcasts.
I agree. It's very hard to get a case where the same cv protects two
logically distinct predicates if you're going to signal rather than
broadcast.
DS
.
- Follow-Ups:
- Re: Pthread programming: about the lost condition signals.
- From: Markus Elfring
- Re: Pthread programming: about the lost condition signals.
- From: Szabolcs Ferenczi
- Re: Pthread programming: about the lost condition signals.
- From: Dave Butenhof
- Re: Pthread programming: about the lost condition signals.
- References:
- Pthread programming: about the lost condition signals.
- From: climber . cui
- Re: Pthread programming: about the lost condition signals.
- From: Eric Sosman
- Re: Pthread programming: about the lost condition signals.
- From: climber . cui
- Re: Pthread programming: about the lost condition signals.
- From: Eric Sosman
- Re: Pthread programming: about the lost condition signals.
- From: David Schwartz
- Re: Pthread programming: about the lost condition signals.
- From: Szabolcs Ferenczi
- Pthread programming: about the lost condition signals.
- Prev by Date: Re: Pthread programming: about the lost condition signals.
- Next by Date: Re: Pthread programming: about the lost condition signals.
- Previous by thread: Re: Pthread programming: about the lost condition signals.
- Next by thread: Re: Pthread programming: about the lost condition signals.
- Index(es):
Relevant Pages
|