C++ Logo

std-proposals

Advanced search

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

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Sun, 18 Jun 2023 17:40:17 -0400
On Sun, Jun 18, 2023 at 5:34 PM Ville Voutilainen
<ville.voutilainen_at_[hidden]> wrote:
>
> On Mon, 19 Jun 2023 at 00:17, Jason McKesson via Std-Proposals
> <std-proposals_at_[hidden]> wrote:
> >
> > On Sun, Jun 18, 2023 at 4:26 PM Smith, Jim via Std-Proposals
> > <std-proposals_at_[hidden]> wrote:
> > >
> > > Hi All,
> > >
> > > It would be convenient if std::thread would automatically become not joinable after the thread function ends.
> >
> > This just creates a race condition:
> >
> > ```
> > if(thread.joinable())
> > thread.join();
> > ```
> >
> > Is this functioning code? It looks like it *should* be but it isn't.
> > The answer to the `joinable` question is out-of-date the moment after
> > you call the function. If the thread function exits *after* the
> > joining thread asks if it is joinable, then the call to `join` will
> > fail.
>
> Why would the call to join() fail? It's perfectly okay to call join()
> after the thread
> function has exited.

We're assuming the OP's idea were adopted. If a thread is not
"joinable" after the thread function ends, that *must* mean that
`join` will fail in that circumstance. Otherwise, what does "joinable"
even mean? It's even part of the definition of `join` that you get an
exception if `joinable` is false.

Received on 2023-06-18 21:40:32