[c++-pthreads] Re: [SPAM] - Re: [c++-pthreads] Re: FW: RE: Re: I'm Lost - Email found in subject

Ted Baker baker at cs.fsu.edu
Fri Mar 17 16:12:00 UTC 2006


> This is the first justification I've seen for Ulrich's position.  Can 
> you elaborate on how allowing us to reset the cancelled bit could lead 
> to a race condition? -- Jason

The potential race is between resetting the bit and a new
cancellation request, potentially missing a new cancellation
request.

From an implementors point of view, depending on how your thread
implementation does other things internally, if protecting this
bit requires adding a second per-thread lock, you have a problem
with potential deadlock.  I seem to recall running into this
problem, years ago, when I was working on such things.

From an applications point of view, allowing threads
once-cancelled to again become "virgin" is also semantically bad,
because of indeterminacy:

1. Some therad B tries to cancel a thread A.
It locks the cancellation state of A, and sets a bit
to indicate that A should cancel.

I'm assuming this is because of some condition detected by another
thread (or process), maybe a timeout because the thread A has not
responded for a while.

2. Some concurrent thread C, who also noticed that the thread is
not responding, also tries to cancel A.  It locks the cancellation
state of A, and sets the cancellation pending bit.

We now have three possible scenarios, in a race:

3a. (2) takes effect before A starts recovery,
and so (1) and (2) are merged into a single exception
from A's point of view.

3b. (2) takes effect while A is executing exception
handler code.  Depending on implementation details,
the second cancellation may be merged with the first,
or it may appear as a new pending cancellation event,
to be detected as soon as A becomes cancellable again.

3c. (2) takes effect after A finished executing the
handler code, and has resumed normal execution.
A new cancellation process begins for A at the
next point it is cancellable.

The standard C thread cancellation semantics, by not allowing
a thread to return to normal after cancellation, eliminate
possibilities (3b) and (3c), so the outcome of 
multiiple cancellation attempts is deterministic.

--Ted



More information about the c++-pthreads mailing list