https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128548
Bug ID: 128548
Summary: std::thread::join should check to make sure id is not
__gthread_self before calling __gthread_join
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: libstdc++
Assignee: unassigned at gcc dot gnu.org
Reporter: pinskia at gcc dot gnu.org
Target Milestone: ---
Created attachment 66109
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=66109&action=edit
testcase that should always throw
As reported in https://gcc.gnu.org/pipermail/gcc-bugs/2026-November/981159.html
:
Right now from the way I understand it. The attached testcase will cause a
deadlock (hang) rather than throwing an exception.
There are 2 ways of fixing this. Either inside the pthread wrapper (in
gthr-posix.h) change __gthread_join to check if the id is not self and return
an error. Or inside thread::join in ./src/c++11/thread.cc check to make sure
that the id is not the self.
Both the win32 and vxworks __gthread_join already has the check against the
current thread id, so it might make sense to just do the same in the posix one.
Win32:
```
int
__gthr_win32_join (__gthread_t thr, void **value_ptr)
{
int status = 0;
if (GetThreadId ((HANDLE) thr) == GetCurrentThreadId ())
return 1;
```
vxworks:
```
int
__gthread_join (__gthread_t __threadid, void **__value_ptr)
{
if (!__threadid)
return ERROR;
/* A thread cannot join itself. */
if (__threadid->task_id == taskIdSelf ())
return ERROR;
```