Why are globals set to tls by default? and why is fast code ugly by default?

Jacob Shtokolov jacob.100205 at gmail.com
Mon Mar 27 11:14:48 UTC 2023


On Sunday, 26 March 2023 at 18:07:03 UTC, ryuukk_ wrote:
> What i do not understand is the reasoning behind choosing tls 
> global by default in D

Because the language maintainers decided that they want to 
emphasize the actor model with no default shared state in the 
language.

This is quite beneficial, BTW, as multiprocessor programming is 
by no means an easy problem, so the language just pushes you 
towards the error-free code by default.

As for `__gshared`, this is intended only for interfacing with C 
and other languages where global variables is the default way of 
doing things.

This may seem ugly, but if you write your program completely in D 
and want to avoid TLS, why don't you avoid globals at all?

Just allocate some state on the stack and pass it to your 
functions as `ref`.

This way it will actually be much cleaner and easy to understand 
and test, because global variables are tempting to be mutated, 
which makes the program control flow much less transparent.



More information about the Digitalmars-d-learn mailing list