Local data warehouse variables in D, confusion regarding interaction with module initializers

Say I have some global variables and some initializable stream variables (e.g. TLS a , global b , TLS c and global d initialized in this order) in the static module this () and deinitialized in its static ~ this ().

Can someone let me through the steps taken by the program to initialize variables in both single-user and multi-threaded applications?

Or correct me if I make a really terrible assumption?

EDIT: Make it a little clearer:

 module mymodule; int a; __gshared int b; int c; __gshared d; static this() { a = 2; b = 3; c = 4; d = 1337; } static ~this() { if(a == 2) dosomefunc(b); // and other nonsensical things that involve branching on the TLS and using the globals. } 

What happens when I change the value of a in some thread that was generated but never touched it in the main thread? Is dosomefunc() ever called? What is the actual behavior that should be here, and what does the behavior depend on? How are module initializers invoked with respect to TLS? Is it called once once and TLS vars get the shaft outside of the value initialization? What do these two (de) initializers in the world mean?

+4
source share
2 answers

There are two types of static constructors:

 static this() { ... } 

which starts whenever a new thread starts. It is designed to initialize local thread storage (TLS) variables.

 shares static this() { ... } 

It is launched once at program startup and is used to initialize __gshared and other global data.

+2
source

Well, after repeatedly searching and writing some test code, I think I have a descriptor, with no details of __gshared variables.

From here , the static module constructor is run once in the thread. Its static deconstructor works when the thread returns. Modules act as pseudo-random "instance" streams, with their member variables being their TLS variables, and their __gshared variables are like static variables for this class. In my opinion, this forms a relationship

object: class :: module : [logical collection of variables __gshared ]

and

[no-qualifier]: static :: [no-qualifier]: __gshared

where the last half of each relationship is formed by multithreading and static constructors / destructors.

As a result, I realized that __gshared fits into the module initialization.

I hope someone else comes up with a much better answer filling the hole that I can accept.

0
source

Source: https://habr.com/ru/post/1496254/


All Articles