This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

std::thread::id comparisons


All the comparison operators on our std::thread::id rely on undefined
behaviour because our thread::id is just a pthread_t.

1) operator< is not defined for pthread_t (it happens to work on our
primary platforms where pthread_t is an integer)

2) operator== uses pthread_equal, which is undefined for invalid
thread IDs, POSIX says:

       If either t1 or t2 are not valid thread IDs, the behavior is undefined.

The pthread_equal issue means that comparing to a default-constructed
thread::id (e.g. in thread::joinable()) is not portable.

The attached patch is a prototype implementation using size_t for
thread::id, which means we can safely compare and hash thread::id.
This requires moving the pthread_t that used to be in the thread::id
into thread::_Impl and thread objects need to keep a pointer to the
Impl rather than just keeping the id.

Rather than adding members to _Impl_base (and breaking any clients who
compiled against the old definition and call thread::_M_start_thread
in the library .so) I defined a new _Impl_base2 and used that.
Clients compiled against the old code can still link to and call
_M_start_thread but they'll get an EPERM exception.

I actually prefer our current implementation, because having
thread::id and pthread_t compatible has some nice benefits, but I
mentioned this patch in the "typeinfo tuning" thread so I thought I
should share the code even if it's not ready.

An alternative to this rework would be to get POSIX to define an
invalid pthread_t value, with equality and less than operations that
can handle that invalid value, and a hash function for pthread_t.
Even if that happened, it wouldn't help non-POSIX platforms (and I
assume we intend to provide full C++0x support for them eventually.)

Attachment: thread_id.patch
Description: Binary data


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]