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

Anthony Williams anthony at justsoftwaresolutions.co.uk
Mon Nov 15 13:57:11 GMT 2010


On 13/11/10 04:16, Boehm, Hans wrote:
> I'm a bit concerned that we're talking about a bunch of different
> things here.  I think the most worrisome scenario for this issue is a
> program that invokes a function with async() and then turns the
> result into a shared_future, and makes the result f available to
> multiple threads.  If the asynchronously run function throws an
> exception, then that exception e may be thrown in several different
> threads calling f.get().  If one of those threads modifies e, we have
> a data race.

Yes, that's exactly my concern.

> 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?

I've seen code that modifies the exception, e.g. to add call stack info:

void f(int arg1,int arg2){
    try{
       g();
    catch(my_exception& e)
    {
       e.add_caller("f(int,int)",arg1,arg2);
       throw;
    }
}

How common this is in practice, and how many other uses people have for 
modifying exceptions we can't tell.

My big concern here is that the data race might be added without 
knowledge of the code affected. If I change g() above to wait on a 
shared_future and let any exceptions propagate up then f() is now 
potentially exposed to a data race, and I haven't touched f().

Worse, this code may be race free when compiled with one compiler (e.g. 
MSVC 2010) and acquire a data race when recompiled with a different 
compiler (e.g. gcc).

> The argument for this resolution is that it appears to be the only
> thing implementable in the Itanium ABI, which I believe is commonly
> used on other platforms, like X86-64.  Breaking the exception ABI
> seems like a very high cost, so I think we'd need at least a more
> solid example of breakage than I've seen so far.

I appreciate that there is a cost. Personally, I think it's worth it: 
introducing data races is a serious issue.

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