Topic
4 replies Latest Post - ‏2010-10-19T07:45:04Z by SystemAdmin
SystemAdmin
SystemAdmin
196 Posts
ACCEPTED ANSWER

Pinned topic std::uncaught_exception() behavior in the context of multi-threading

‏2010-09-14T08:42:25Z |
Hi,

I'm using xlC V9.0 for AIX on a multi-threaded C++ project. I recently discovered that during exception propagation in one thread, std::uncaught_exception() returns true in all other threads, even when no exception handling is taking place in those threads. See the code example below; with xlC V9.0 on AIX, this prints several "Oopses"; with gcc 4.2.1 on Mac OS X, all is well.

This seems a compiler error to me, or at least a quality of implementation issue. Even though the current C++ standard doesn't mention multi-threading, and the latest draft of the upcoming standard isn't explicit on how std::uncaught_exception() should behave in the context of multiple threads, it seems clear (well, to me) that std::uncaught_exception() should return true/false on a per-thread basis. See also http://www.codesourcery.com/public/cxx-abi/cxx-closed.html#D8 (where IBM was involved in the discussion).

I was wondering: would the xlC-team regard this an error, too? If so: has this been fixed already in more recent versions of xlC (I don't have acces to any more recent versions than V9.0, unfortunately)?

Thanks,
René


#include <unistd.h> #include <iostream> #include <stdexcept> #include <boost/thread.hpp>   struct DelayedDestruction 
{ ~DelayedDestruction() 
{ sleep(4); 
} 
};   

void thread1() 
{ 

try 
{ sleep(3); DelayedDestruction d; 

throw 1; 
} 

catch(...)
{ 
} 
}   

void thread2() 
{ 

for( 

int i=0; i!=10; ++i )
{ 

if( std::uncaught_exception() )
{ std::cout << 
"Oops: uncaught exception detected in thread2()\n"; 
} 

else 
{ std::cout << 
"All well in thread2()\n"; 
} sleep(1); 
} 
}   

int main() 
{ boost::thread_group threads; threads.create_thread( thread1 ); threads.create_thread( thread2 );   threads.join_all();   

return 0; 
}
Updated on 2010-10-19T07:45:04Z at 2010-10-19T07:45:04Z by SystemAdmin
  • Michael_Wong
    Michael_Wong
    29 Posts
    ACCEPTED ANSWER

    Re: std::uncaught_exception() behavior in the context of multi-threading

    ‏2010-09-23T19:53:13Z  in response to SystemAdmin
    Hi, thank-you for posting. I agree the behavior is less then desirable on AIX, although it it implementation defined. You are correct in that we discussed this issue the C++ ABI times, and in fact I tested it on our Linux xlC V11.1 compiler and it does work as follows:

    All well in thread2()
    All well in thread2()
    All well in thread2()
    All well in thread2()
    All well in thread2()
    All well in thread2()
    All well in thread2()
    All well in thread2()
    All well in thread2()
    All well in thread2()

    I am happy about this result as our Linux xlC compiler is C++ ABI compatible.
    When I tried on AIX, even with GNU I got:
    All well in thread2()
    All well in thread2()
    All well in thread2()
    Oops: uncaught exception detected in thread2()
    Oops: uncaught exception detected in thread2()
    Oops: uncaught exception detected in thread2()
    Oops: uncaught exception detected in thread2()
    Oops: uncaught exception detected in thread2()
    All well in thread2()
    All well in thread2()

    Our AIX xlC compilers are not C++ ABI compatible, so in some ways this result does not make it any more invalid. If you like to go over the behavior in more detail, we should exchange emails and talk about this.

    Thanks.
    • SystemAdmin
      SystemAdmin
      196 Posts
      ACCEPTED ANSWER

      Re: std::uncaught_exception() behavior in the context of multi-threading

      ‏2010-10-13T10:37:01Z  in response to Michael_Wong
      Hi Michael,

      Thanks for your response. In fact, I'd like to discuss this a little bit further, so I replied to the email with your response, sent from dWForums(at)us.ibm.com, but I didn't get a response. Perhaps this email address is not monitored. Could you contact me, or let me know through the forum how I can contact you? Alternatively, we could continue the discussion in the forum (could be interesting for others too, now or in the future).

      Thanks,
      René
      • Michael_Wong
        Michael_Wong
        29 Posts
        ACCEPTED ANSWER

        Re: std::uncaught_exception() behavior in the context of multi-threading

        ‏2010-10-15T14:14:14Z  in response to SystemAdmin
        Thank-you for bringing this up. This is a topic of great interest to us too. I will try to contact you privately and summarize on this forum.
        • SystemAdmin
          SystemAdmin
          196 Posts
          ACCEPTED ANSWER

          Re: std::uncaught_exception() behavior in the context of multi-threading

          ‏2010-10-19T07:45:04Z  in response to Michael_Wong
          OK, thanks for your response.

          René