]> gcc.gnu.org Git - gcc.git/commitdiff
libstdc++: Throw instead of segfaulting in std::thread constructor [PR 67791]
authorJonathan Wakely <jwakely@redhat.com>
Tue, 24 Nov 2020 12:48:31 +0000 (12:48 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Fri, 28 Apr 2023 12:31:16 +0000 (13:31 +0100)
This turns a mysterious segfault into an exception with a more useful
message. If the exception isn't caught, the user sees this instead of
just a segfault:

terminate called after throwing an instance of 'std::system_error'
  what():  Enable multithreading to use std::thread: Operation not permitted
Aborted (core dumped)

libstdc++-v3/ChangeLog:

PR libstdc++/67791
* src/c++11/thread.cc (thread::_M_start_thread(_State_ptr, void (*)())):
Check that gthreads is available before calling __gthread_create.

(cherry picked from commit 4bbd5d0c5fb2b7527938ad44a6d8a2f2ef8bbe12)

libstdc++-v3/src/c++11/thread.cc

index 2d8781724f24e9fa8eec42c17d2888a36c76529f..b175ad5413e107066da47aca91a2bd1309fc305a 100644 (file)
@@ -132,6 +132,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
   void
   thread::_M_start_thread(_State_ptr state, void (*)())
   {
+    if (!__gthread_active_p())
+      {
+#if __cpp_exceptions
+       throw system_error(make_error_code(errc::operation_not_permitted),
+                          "Enable multithreading to use std::thread");
+#else
+       __builtin_abort();
+#endif
+      }
+
     const int err = __gthread_create(&_M_id._M_thread,
                                     &execute_native_thread_routine,
                                     state.get());
This page took 0.059435 seconds and 5 git commands to generate.