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

Anthony Williams anthony at justsoftwaresolutions.co.uk
Fri Nov 12 13:27:20 GMT 2010


On 12/11/10 12:18, N.M. Maclaren wrote:
> On Nov 12 2010, Anthony Williams wrote:
>>
>> 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.
>
> It's FAR more serious than what is nowadays called 'thread safety'!
>
>> 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.
>
> That's horrific! It is also a stake through the coffin (not just a nail)
> of any implementation that attempts to support system-generated exceptions
> as C++ exceptions, or even the cleanup of any system state during handlers.

This facility has been in the C++0x draft for a while now (it was in the 
August 2007 working draft, N2369), and is now in at least two shipping 
compilers: MSVC2010 and g++ 4.4 and later.

Just to avoid confusion: exceptions are only transferred between threads 
if you explicitly capture them in an exception_ptr object either 
directly or by wrapping a function in a packaged_task, and then 
explicitly rethrow in another thread either with std::rethrow_exception, 
or by calling some_future.get().

> The point is that genuine exceptions often include thread-specific data,
> often of a form that CANNOT be transferred between threads.

This issue (1369) proposes that rethrowing must always copy the 
exception. This would therefore enable such implementations to copy the 
appropriate data in the exception copy constructor.

> C++ doesn't support resumption, which is something that REALLY can't be
> done if you pass exceptions between threads, but what about cleanup of
> (say) attached GPUs? I am involved with a course where the students are
> being taught to use GPUs from C++, so it's a real-life problem. Passing
> exceptions between threads is a classic no-no - when I queried it at some
> WG21 meeting, I was told that it had been positively decided against.
>
> Please rethink. Forbidding that practice is the only sane solution.

I disagree; there are many uses for passing exceptions between threads. 
In any case I seriously doubt the feature will be removed from C++0x at 
this late stage.

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