This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

std::thread should catch abi::__forced_unwind


If a function run by a std::thread exits with an uncaught exception
then the program is terminated, which means thread cancellation
terminates the program if the thread was started by std::thread.  i.e.
this (non-portable) program works fine unless CXX is defined, in which
case it segfaults in std::terminate.

#include <pthread.h>
#include <thread>
#include <atomic>

std::atomic<bool> started{ false };

extern "C" void* f(void*)
{
  started = true;
  while (true)
    std::this_thread::sleep_for(std::chrono::seconds(1));
  return nullptr;
}

void cancel_and_join(std::thread& t)
{
  pthread_cancel(t.native_handle());
  t.join();
}

void cancel_and_join(pthread_t t)
{
  pthread_cancel(t);
  pthread_join(t, nullptr);
}

int main()
{
#ifdef CXX
  std::thread t(f, nullptr);
#else
  pthread_t t;
  pthread_create(&t, nullptr, &f, nullptr);
#endif
  while (!started)
    std::this_thread::sleep_for(std::chrono::seconds(1));
  cancel_and_join(t);
}

I think we need to ensure the __forced_unwind is rethrown:

--- a/libstdc++-v3/src/c++11/thread.cc
+++ b/libstdc++-v3/src/c++11/thread.cc
@@ -26,6 +26,7 @@
 #include <thread>
 #include <system_error>
 #include <cerrno>
+#include <cxxabi.h>

 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)

@@ -80,6 +81,10 @@ namespace std _GLIBCXX_VISIBILITY(default)
        {
          __t->_M_run();
        }
+      __catch(const abi::__forced_unwind&)
+       {
+         throw;
+       }
       __catch(...)
        {
          std::terminate();


Any objections or comments before I make that change?


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]