[Bug libstdc++/89591] How can thread.joinable() reliably work if the pthread_t id is not initialized?

2019-03-14 Thread redi at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89591

Jonathan Wakely  changed:

   What|Removed |Added

 Status|UNCONFIRMED |RESOLVED
 Resolution|--- |INVALID

--- Comment #3 from Jonathan Wakely  ---
OK, closing.

[Bug libstdc++/89591] How can thread.joinable() reliably work if the pthread_t id is not initialized?

2019-03-14 Thread bique.alexandre at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89591

--- Comment #2 from Alexandre Bique  ---
> It's initialized here:
> 
>> id() noexcept : _M_thread() { }

I did not know that it would initialize it to 0.

In that case let's hope that pthread_t == "value-initialized" will never
happen.

Thank you for answering my question, and I think we can close the bug.

Regards,
Alex

[Bug libstdc++/89591] How can thread.joinable() reliably work if the pthread_t id is not initialized?

2019-03-14 Thread redi at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89591

--- Comment #1 from Jonathan Wakely  ---
(In reply to Alexandre Bique from comment #0)
> Hi,
> 
> I've been reading the  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;
> 
>   friend bool
>   operator==(thread::id __x, thread::id __y) noexcept;
> 
>   friend bool
>   operator<(thread::id __x, thread::id __y) noexcept;
> 
>   template
>   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;