immutable, static, __gshared, TLS, and compile time allocation

Michel Fortin michel.fortin at michelf.com
Fri Apr 20 17:05:19 PDT 2012


On 2012-04-20 20:45:11 +0000, Manu <turkeyman at gmail.com> said:

> I need to clarify some things that have confused me a few times. I've
> reconsidered these basic attributes a few times, and I thought I understood
> them, but I obviously don't.
> 
> The case that has confused me is here:
> http://d.puremagic.com/issues/show_bug.cgi?id=7897
> 
> In the global scope:
> 
> int x; <- x is TLS
> 
> but:
> 
> static int x;  <- this is... a 'static' global instance, whatever that
> means? Is this TLS or not?

If you're trying to make a C-like global, write this:

	static __gshared int x;

"static" here is optional if you are at global scope. "__gshared" is a 
storage class which basically mean "no TLS": the global variable is 
available to all threads (C-style).

Note that because "__gshared" is a storage class (and not a type 
constructor), the type of x is "int", not "shared(int)". So, despite 
the same address being seen by all threads, it'll be treated as if it 
was a thread-local variable as far as the type system is concerned.

One could say that shared and immutable global variables are implicitly 
"__ghsared", in the sense that they are not in TLS, because they have a 
type qualifier that make it safe to be visible in all threads.

-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/



More information about the Digitalmars-d mailing list