Add check for std::thread::join() on that thread [PR128548]
std::thread::join() now prevents self-joining to match POSIX behavior and Glibc documentation.
This commit adds a check to std::thread::join() to prevent a thread from joining itself, addressing a potential undefined behavior scenario. While Glibc’s implementation typically handles this by reporting an error, this change makes the C++ standard library’s behavior more robust and compliant with POSIX standards.
In Details
The std::thread::join() method in libstdc++ now includes a self-join detection mechanism. This is critical because POSIX specifies that pthread_join() results in undefined behavior if the calling thread attempts to join itself. Although Glibc's underlying pthread_join implementation might catch this, explicitly adding the check in thread.cc enhances portability and adheres to the C++ standard's threading requirements.
- std::thread::join()
- A member function of the C++
std::threadclass that blocks the calling thread until the associated thread (*this) finishes its execution. - POSIX
- Portable Operating System Interface. A family of standards specified by the IEEE for maintaining compatibility between operating systems.
- Undefined Behavior
- Behavior that results from the execution of C++ code that violates a system's preconditions and for which the C++ standard imposes no requirements. Programs exhibiting undefined behavior may behave unexpectedly on any platform or with any compiler.