Stream: general

Topic: Attached threads whose parent exits


RalfJ (Apr 19 2020 at 17:01, on Zulip):

Does anyone know if POSIX (or other concurrency APIs) say anything about what happens when a thread is attached to a parent thread, and the parent thread exits first? I tried to find out from the docs but didn't find a clear statement. Maybe I was looking in the wrong place.
(See here for the context for this question.)
Cc @bjorn3 who originally mentioned this.

bjorn3 (Apr 19 2020 at 17:12, on Zulip):

I may have remembered it wrong. At least exiting from the main thread kills the other threads though.

bjorn3 (Apr 19 2020 at 17:16, on Zulip):

Except when calling pthread_exit, in which case the other threads continue.

bjorn3 (Apr 19 2020 at 17:17, on Zulip):

https://computing.llnl.gov/tutorials/pthreads/#CreatingThreads Link: https://computing.llnl.gov/tutorials/pthreads/#CreatingThreads

RalfJ (Apr 19 2020 at 17:26, on Zulip):

lucky enough pthread_exit is not part of this proposal :D

RalfJ (Apr 19 2020 at 17:30, on Zulip):

also Cc @Vytautas Astrauskas

RalfJ (Apr 19 2020 at 17:30, on Zulip):

hm so these "resources" that need to be freed, does that mean if we do not detach a thread and do not join it there is a leak? could we make miri detect that leak?

RalfJ (Apr 19 2020 at 17:31, on Zulip):

also is the parent thread relationship used for anything? doesnt seem like it, any thread can join any other thread I think...

RalfJ (Apr 19 2020 at 17:31, on Zulip):

however, " It is a logical error to attempt multiple joins on the same thread. "

RalfJ (Apr 19 2020 at 17:31, on Zulip):

we should catch that as UB

Amanieu (Apr 19 2020 at 18:30, on Zulip):

A pthread_t join handle isn't "owned" by any particular thread in pthreads. It can be freely sent from one thread to another.

Amanieu (Apr 19 2020 at 18:31, on Zulip):

Calling pthread_detach or pthread_join effectively "consumes" the pthread_t, it becomes invalid afterwards.

Amanieu (Apr 19 2020 at 18:32, on Zulip):

Exiting from main() will kill all other threads as if exit() was called. However if the main thread calls pthread_exit then other threads continue running.

RalfJ (Apr 20 2020 at 07:39, on Zulip):

Amanieu said:

A pthread_t join handle isn't "owned" by any particular thread in pthreads. It can be freely sent from one thread to another.

okay so I take this to mean that the parent thread relationship is entirely irrelevant

RalfJ (Apr 20 2020 at 07:39, on Zulip):

Amanieu said:

Exiting from main() will kill all other threads as if exit() was called. However if the main thread calls pthread_exit then other threads continue running.

what does this mean for TLS dtors? do they get run for the "killed" threads?

Amanieu (Apr 20 2020 at 11:29, on Zulip):

No, I don't think so.

Amanieu (Apr 20 2020 at 11:29, on Zulip):

Actually, I am 100% sure they are not executed on the killed threads.

Amanieu (Apr 20 2020 at 11:29, on Zulip):

Whether they are executed on the main thread is a bit blurry: #28129

RalfJ (Apr 20 2020 at 16:52, on Zulip):

Amanieu said:

Whether they are executed on the main thread is a bit blurry: #28129

ah, that's interesting... miri has run them so far. I guess that's the conservative choice here; if there are leaks in the real execution because TLS dtors dont run they dont matter as the process is quitting anyway. and if the TLS dtors are wrong/UB, this way we catch that.

RalfJ (Apr 20 2020 at 16:53, on Zulip):

so I guess we should run TLS dtors for joined threads, the main thread... and I suppose detached threads when they are done?

bjorn3 (Apr 20 2020 at 16:55, on Zulip):

We shouldn't run them for the main thread when using pthread_exit from the main thread, as the program can continue executing.

RalfJ (Apr 20 2020 at 17:05, on Zulip):

we don't support pthread_exit at all

bjorn3 (Apr 20 2020 at 17:06, on Zulip):

Forgot that

RalfJ (Apr 20 2020 at 17:08, on Zulip):

detached threads are interesting though

Last update: May 29 2020 at 17:55UTC