[Bug libstdc++/89591] How can thread.joinable() reliably work if the pthread_t id is not initialized?
redi at gcc dot gnu.org
gcc-bugzilla@gcc.gnu.org
Thu Mar 14 13:54:00 GMT 2019
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89591
--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Alexandre Bique from comment #0)
> Hi,
>
> I've been reading the <thread> header and I've found that the thread id of
> std::thread is not initialized, I hope that I'm wrong and I missed something.
It's initialized here:
> id() noexcept : _M_thread() { }
> So I wonder how std::thread.joinable() can work?
>
> On top of that, pthread_t has no "invalid thread id" or "uninitialized
> thread id" value. So I wonder how the whole logic can work.
Right. We assume that a value-initialized pthread_t will not be used for any
valid thread. This certainly isn't guaranteed by POSIX.
> To me if your only attribute is pthread_t, you can't know figure anything
> about the thread, and even if the thread exists, there is no guarantee that
> the current std::thread object created it: if you create a object:
> std::thread t; then t.id will be uninitialized, so could very well point to
> an existing thread right?
No, because it's not uninitialized.
In practice pthread_t is either an integer or a pointer in all implementations
I know of, and the zero value is not used for running threads.
>
> Regards,
> Alex
>
>
>
>
> From the header:
>
> thread() noexcept = default;
>
> and also:
>
> typedef __gthread_t native_handle_type;
>
> /// thread::id
> class id
> {
> native_handle_type _M_thread;
>
> public:
>
> explicit
> id(native_handle_type __id) : _M_thread(__id) { }
>
> private:
> friend class thread;
> friend class hash<thread::id>;
>
> friend bool
> operator==(thread::id __x, thread::id __y) noexcept;
>
> friend bool
> operator<(thread::id __x, thread::id __y) noexcept;
>
> template<class _CharT, class _Traits>
> friend basic_ostream<_CharT, _Traits>&
> operator<<(basic_ostream<_CharT, _Traits>& __out, thread::id __id);
> };
>
> private:
> id _M_id;
>
>
>
> and then later:
>
> typedef pthread_t __gthread_t;
More information about the Gcc-bugs
mailing list