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

Anthony Williams anthony at justsoftwaresolutions.co.uk
Fri Nov 12 11:11:52 GMT 2010


Hi,

I am very much concerned about the proposed resolution of this issue. 
Prior to the introduction of std::exception_ptr it was never possible to 
transfer exceptions between threads, and thread safety of exception 
types was not an issue.

With the introduction of exception_ptr and rethrow_exception, it is 
possible to transfer exceptions between threads. In itself, this is a 
good thing. However, exceptions are not local. The code that handles the 
exception may not know that this is an exception that has been transferred.

Up until now, exception handlers could safely assume that they are the 
only code that has access to the given exception object.

Allowing rethrow_exception to reuse the same exception object breaks 
this assumption. It therefore introduces data races in code that **may 
not know the exception was rethrown from another thread**. This code 
thus has no way to deal with this scenario.

I would strongly urge that this issue is reconsidered, and that 
rethrow_exception is required to throw a copy of the original.

A quick summary of the existing practice I know of:

The exception_ptr implementation that ships with MSVC2010 copies the 
exception; the rethrow_exception implementation that ships with gcc in 
-std=c++0x mode doesn't. gcc in -std=c++98 mode doesn't support 
exception_ptr, except via an add-on library.

My just::thread implementation for MSVC 2005 and MSVC 2008 always copies 
the exception. My implementation for gcc -std=c++98 also copies the 
exception, but does not preserve the type unless it is one of the 
standard exception types (e.g. std::runtime_error or std::bad_alloc).

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