thread::id with libstdc++
From
Bonita Montero@3:633/280.2 to
All on Wed Apr 2 20:57:44 2025
C++ recognizes a thread ID, which is represented by the class thread
::id. Internally, this class has only one data member in libstdc++,
and that is a variable of type pthread_t. However, thread::id recognizes
a default constructed state that does not refer to any thread. I asked
myself how this works, because all possible values of pthread_t can
refer to a thread. This is what the default constructor of thread::id
looks like:
id() noexcept : _M_thread() { }
This means that thread::id::id assumes the default-constructed state of pthread_t as not belonging to any thread. This is actually outside the specification.
Then I was interested in how two thread::id are compared for equality:
/// @relates std::thread::id
inline bool
operator==(thread::id __x, thread::id __y) noexcept
{
// pthread_equal is undefined if either thread ID is not valid, so we
// can't safely use __gthread_equal on default-constructed values (nor
// the non-zero value returned by this_thread::get_id() for
// single-threaded programs using GNU libc). Assume EqualityComparable.
return __x._M_thread == __y._M_thread;
}
This means that pthread_equal is not used as it should be.
--- MBSE BBS v1.1.1 (Linux-x86_64)
* Origin: A noiseless patient Spider (3:633/280.2@fidonet)