Member variables in method are null when called as delegate from thread

Arafel er.krali at gmail.com
Mon Jan 11 15:42:54 UTC 2021


On 11/1/21 14:42, Steven Schveighoffer wrote:
> 
> That isn't exactly true. Member variables are members of the object. If 
> the object is shared, the member variables are shared. If the object is 
> local the variables are local.
> 
> Thread local really only applies to *static* variables, such as globals 
> or members declared static. If that were the case, yes, the other thread 
> would not see the object.
> 
> I did not respond to the OP because I also don't know why it wouldn't 
> work. But I also don't know what all the code is doing.
> 
> -Steve

Out of curiosity, what happens with members that are declared `shared` 
in a non-shared object?

I thought that declaring an object `shared` "promotes" its members, but 
that it wasn't strictly needed for a non-shared object to have shared 
members, but I might be wrong here.

```
struct S {}

class A {
     S s1;
     shared S s2;
}

void main() {
     A a1 = new A();
     pragma(msg, typeof(a1.s1)); // S
     pragma(msg, typeof(a1.s2)); // shared(S)
     shared A a2 = new shared A();
     pragma(msg, typeof(a2.s1)); // shared(S)
     pragma(msg, typeof(a2.s2)); // shared(S)
}
```

https://run.dlang.io/is/skCfvE

Of course I don't know the practical differences in the actual 
accessibility of the different members beyond the type system.

Is there any way to check if a pointer is actually TLS or global 
storage? If `a1.s2` can't be properly accessed from different threads, 
I'd consider that a big bug in the `shared` implementation.

Best,

A.


More information about the Digitalmars-d-learn mailing list