[PATCH] libstdc++: Add check for std::thread::join() on that thread [PR128548]

Tomasz Kaminski tkaminsk@redhat.com
Tue Nov 10 13:42:48 GMT 2026


On Tue, Nov 10, 2026 at 10:50 AM Jonathan Wakely <jwakely@redhat.com> wrote:

> POSIX says that pthread_join() is undefined if the calling thread tries
> to join itself. Our std::thread::join() does not check for this
> (possible because Glibc documents that it will be detected and an error
> reported).
>
> libstdc++-v3/ChangeLog:
>
>         PR libstdc++/128548
>         * src/c++11/thread.cc (thread::join): Check for self-join.
>         * testsuite/30_threads/thread/members/128548.cc: New test.
> ---
>
> In bugzilla Andrea suggested adding this check to the gthr-posix.h
> version of __gthread_join, as it's already present in (IIRC) the win32
> and vxworks versions. This seems simpler.
>
Yes, seem simpler. But I think it would be better for other projects under
GCC
umbrealla (I see it in libfortran), if __gthread_join provides consistient
interface
across platforms. My preference would be to add the test there, and maybe
return EDEADLK consistently.

>
> Tested x86_64-linux.
>
>  libstdc++-v3/src/c++11/thread.cc              |  8 ++++--
>  .../30_threads/thread/members/128548.cc       | 27 +++++++++++++++++++
>  2 files changed, 33 insertions(+), 2 deletions(-)
>  create mode 100644
> libstdc++-v3/testsuite/30_threads/thread/members/128548.cc
>
> diff --git a/libstdc++-v3/src/c++11/thread.cc
> b/libstdc++-v3/src/c++11/thread.cc
> index 16b0d90d024b..881346428e81 100644
> --- a/libstdc++-v3/src/c++11/thread.cc
> +++ b/libstdc++-v3/src/c++11/thread.cc
> @@ -129,9 +129,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>    void
>    thread::join()
>    {
> -    int __e = EINVAL;
> +    int __e = 0;
>
> -    if (_M_id != id())
> +    if (_M_id == this_thread::get_id())
> +      __e = EDEADLK;
> +    else if (_M_id == id())
> +      __e = EINVAL;
> +    else
>        __e = __gthread_join(_M_id._M_thread, 0);
>
>      if (__e)
> diff --git a/libstdc++-v3/testsuite/30_threads/thread/members/128548.cc
> b/libstdc++-v3/testsuite/30_threads/thread/members/128548.cc
> new file mode 100644
> index 000000000000..86a7e63c8090
> --- /dev/null
> +++ b/libstdc++-v3/testsuite/30_threads/thread/members/128548.cc
> @@ -0,0 +1,27 @@
> +// { dg-do run { target c++11 } }
> +// { dg-additional-options "-pthread" { target pthread } }
> +// { dg-require-gthreads "" }
> +
> +// Bug 128548 - std::thread::join should check to make sure id is not self
> +
> +#include <thread>
> +#include <system_error>
> +#include <testsuite_hooks.h>
> +
> +int main()
> +{
> +  std::error_code ec;
> +  std::thread t;
> +  t = std::thread{ [&] {
> +      try
> +       {
> +         t.join();
> +       }
> +      catch (const std::system_error& e)
> +       {
> +         ec = e.code();
> +       }
> +  }};
> +  t.join();
> +  VERIFY( ec == std::errc::resource_deadlock_would_occur );
> +}
> --
> 2.56.0
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://gcc.gnu.org/pipermail/libstdc++/attachments/20261110/fe4aa946/attachment.htm>


More information about the Libstdc++ mailing list