C++ Logo

std-proposals

Advanced search

Re: [std-proposals] std::thread::first_ever

From: Thiago Macieira <thiago_at_[hidden]>
Date: Sun, 30 Apr 2023 09:15:27 -0700
On Sunday, 30 April 2023 05:14:08 PDT Frederick Virchanza Gotham via Std-
Proposals wrote:
> I just realised now that this isn't supposed to happen. It's possible
> with pthreads, but you'll be missing all the destructors and 'atexit'
> stuff that would normally be invoked after main:

POSIX seems to imply that this is *not* UB.
https://pubs.opengroup.org/onlinepubs/7908799/xsh/pthread_exit.html

This says:
> The process exits with an exit status of 0 after the last thread has been
> terminated. The behaviour is as if the implementation called exit() with a
> zero argument at thread termination time.

That only makes sense if the main() thread is allowed to have exited, and as
we know, returning from main() causes the entire program to terminate all its
threads.

It also about atexit() routines, which only makes sense for the main() thread,
though I think this wording is there to prohibit implementations that would
implement pthreads using separate processes that share memory (like the
original LinuxThreads implementation in glibc 2.0). However, in the context of
the other paragraph, it would bless the behaviour that exists, saying that if
you've pthread_exit()ed the main thread, then there's nothing to release
process-level resources, unless you explicitly call exit() somewhere.

In any event, we don't have pthread_exit() in the C++ standard.

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

Received on 2023-04-30 16:15:30