C++ Logo


Advanced search

Re: [std-proposals] Make std::thread not joinable after thread function ends.

From: Thiago Macieira <thiago_at_[hidden]>
Date: Sun, 18 Jun 2023 15:42:46 -0700
On Sunday, 18 June 2023 14:45:52 PDT Sebastian Wittmeier via Std-Proposals
> Jim was suggesting, that a finished thread would return .joinable() as false

And, like I posted, it hasn't finished until you've called join(), so the
request for change is no change at all.

> One could then expect (according to the name) that .join() would fail then.

And it does: if you try to join() a thread that has already been join()ed or
that was detach()ed from, you get a system_error.

> Alternatively one could suggest to introduce a new non-blocking function to
> ask, whether a thread has finished execution.

Here's a compliant, non-racy implementation:

  bool try_join() { return false; }

Because pthread_tryjoin_np or pthread_timedjoin_np are not portable, users
have to accept the above as a likely implementation.

Mind you, try_join or a timed try_join may still have valid use-cases, but
that's besides the point of this (unworkable) proposal.

Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel DCAI Cloud Engineering

Received on 2023-06-18 22:42:48