[cpp-threads] Issue 1369 --- thread safety of std::rethrow_exception

Anthony Williams anthony at justsoftwaresolutions.co.uk
Mon Nov 15 14:07:57 GMT 2010


On 13/11/10 15:01, Detlef Vollmann wrote:
> On Sat, 13 Nov 2010 04:16:42 +0000 "Boehm, Hans"<hans.boehm at hp.com>
> wrote:
>
>> I'm unclear about (a) why you would want to modify the exception,
>> and (b) how common that is in practice.  Can someone enlighten me?
>> I normally think of exceptions as information that's read by the
>> handler.  Is there another scenario that I'm missing?  Is there a
>> common use case that we break?

> One possible use case for modifying the exception object might be to
> use the exception object for some housekeeping, i.e. you register all
> the objects that have already been cleaned up correctly. The
> interesting point here is that for this you really want one single
> exception object, and not multiple copies floating around.

Would you really expect that to work across multiple threads?

> I think if you really want a copy, you should make it yourself (using
> some clone() function).

Please, no. If we do that then pretty much every use of 
shared_future::get() where the exception is allowed to propagate must 
somehow clone the exception (the type of which we don't know) in order 
to avoid introducing data races to the caller.

> If you get a copy, it's impossible to go back
> to the original exception object.

Agreed. We shouldn't be able to get back to the original exception 
object. If you want to share data between copies then store a 
shared_ptr<important_shared_data> in the exception object, so all copies 
refer to the same data.

> I have to admit that I don't know whether we currently allow
> (multiple) copies, but if we do, I think that's wrong.

std::current_exception is allowed to copy the exception, so you can't 
ever rely on having the original exception object.

std::rethrow_exception "throws the exception object to which p refers".

I think it is reasonable to interpret that as being equivalent to the 
code "throw x;" where x is the stored exception object; this naturally 
copies x as part of the throw. This is the MSVC2010 interpretation.

It is also reasonable to interpret that as an instruction to start stack 
unwinding without copying the stored exception. This is the gcc 
interpretation.

Anthony
-- 
Author of C++ Concurrency in Action     http://www.stdthread.co.uk/book/
just::thread C++0x thread library             http://www.stdthread.co.uk
Just Software Solutions Ltd       http://www.justsoftwaresolutions.co.uk
15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No. 5478976



More information about the cpp-threads mailing list