[Issue 19556] main thread static ctor/dtor should run both shared and thread-local simultaneously

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Jan 8 16:44:46 UTC 2019


--- Comment #1 from Steven Schveighoffer <schveiguy at yahoo.com> ---
Working on a PR for this, and I've run into a couple of snags.

1. Because thread local and shared static ctors will be combined, it will cause
cycles in code which has no cycles if you run them separate. This means we need
to deprecate the behavior, and introduce it under a DRT switch, potentially
permanently as non-default. This may cause cycles in Phobos, haven't checked

2. There is an existing artificial requirement that the TLS destructors of the
main thread run before any of the child threads. However, this is not a correct
requirement. It stems from issue #11309.

Regarding item 2, what happens is this:

1. The main thread's TLS destructors are run.
2. The TLS destructor in std.concurrency sends a message to all owned threads
that the thread is shutting down.
3. All threads waiting on OwnerTerminated exit (hopefully?), because the
message will cascade down to all child threads as they exit and run TLS
4. All child threads are joined in reverse order of creation, but that order
really doesn't matter.

However, this requirement is incorrectly identified in the test case here:

as "Owner threads should run TLS before child threads". There's no such actual
requirement, and in fact, all threads EXCEPT daemon threads run TLS destructors
as part of execution (after the thread function exits), not dependent on when
child or owner threads run theirs.

What really is required is that the main thread should notify all child thread
mailboxes that the owner is terminating. It doesn't have to do so via TLS a
dtor, but this means we need a hook from std.concurrency to druntime. A sort of
"main thread is terminating" hook.

So a more drawn-out change needs to happen. First, we need a registration
system for such things in core.Runtime. Then we need to have std.concurrency
register the shutdown of the mailbox when it is created, or in a shared static
ctor. Finally, we can change the calling of ctor/dtor calls to follow the new


More information about the Digitalmars-d-bugs mailing list