[c++-pthreads] pthread_cancel and EH: let's try this again

Nathan Myers ncm at cantrip.org
Mon Jul 18 22:10:07 UTC 2005


On Mon, Jul 18, 2005 at 08:17:29PM +0300, Peter Dimov wrote:
> Ted Baker wrote:
> >On Mon, Jul 18, 2005 at 06:53:23PM +0300, Peter Dimov wrote:
> >>I'm not sure I follow. In what way is a cancellation exception less
> >>safe than any other exception?
> >
> >In this context, I was thinking of the problem of not knowing when
> >and where such an exception might be thrown.  As others have
> >pointed out, when you are calling a library function written
> >in C (whether part of the standard C library or some other library)
> >you don't know whether it will call something that qualifies as
> >a thread cancellation point.  This is a problem with C, already,
> >and wrapping it in C++ does not make the problem go away.
> 
> In other words, the answer is a qualified "none".
> 
> When you are calling a function written in C++ you don't know whether it 
> will throw unless this is documented.

To have cancellation exceptions come from C functions breaks all code 
that was written assuming C functions don't throw: i.e., all existing 
C++ code that calls C functions.  That's rather more than "none".

It's possible that lots (not all) of exception-safe C++ code would, 
as part of being prepared for exceptions from C++ functions called 
nearby, survive throws from C functions.  It doesn't matter: C 
libraries equipped to exit safely via exception are, for all practical 
purposes, nonexistent.

> It is true that so far we've been able to assume that a function
> written in C doesn't throw when thread cancellation is not in use.
> But we'll still be able to assume that.

Very funny.

> >2) Are exceptions safe, in general?
> 
> We've spent the last decade in learning how to use them safely. It
> doesn't make sense to discard this knowledge.
> 
> When 'new' was changed to throw this broke millions of lines of code,
> and C++ survived the transition. The introduction of cancellation
> points - even into the C part of the standard library - is almost a
> non-event compared to that.

At least four orders of magnitude more C++ code is in production use 
than when the change to operator new took place.  Thread cancellation 
is far from important enough to consider causing even a little of such 
breakage.

Need cancellation only work accidentally (i.e. not) in threads that 
rely on libraries?  If so, cancellation is not really worth discussing: 
C++ code that doesn't use libraries might as well be C code.  If 
cancellation is supposed to be useful for any real programs, common 
libraries must not be broken by it.  We know how to do that.  We need 
not be confused by what POSiX has chosen to do to its own private
version of C.

Nathan Myers
ncm at codesourcery.com 
ncm at cantrip.org



More information about the c++-pthreads mailing list