How to use globals correctly?

Robert M. Münch robert.muench at saphirion.com
Mon Mar 5 19:39:35 UTC 2018


On 2018-03-05 18:57:01 +0000, Steven Schveighoffer said:

> On 3/5/18 1:35 PM, Robert M. Münch wrote:
> 
>> 1. Are myMemb1..N TLS or __gshared as well?
> 
> No, they are on the heap. Only the reference is __gshared. But 
> effectively it is __gshared, since you can reach those items via the 
> global `myObj`.

Ok, that was my idea. And am I right, that I don't need any special 
syntax, just: myObj.myMemb


>> 2. How to best implement a simple global to keep track of values (with 
>> support for threads)?
> 
> Shared is the best mechanism, and you don't have to make it a class, it 
> can be a struct. This is only if you are using it as POD (plain old 
> data).

Ok, in my case I have some methods as well (but maybe can be avoided).

> If you want to have methods, shared kind of sucks. But this at least 
> tells the type system that it's shared between threads.

Why does it suck?

> __gshared does not, it just sticks it in global space, but pretends 
> it's not shared data.

Ok, so this is the really hackish solution...

-- 
Robert M. Münch
http://www.saphirion.com
smarter | better | faster



More information about the Digitalmars-d-learn mailing list