Member variables in method are null when called as delegate from thread
Arafel
er.krali at gmail.com
Mon Jan 11 17:26:00 UTC 2021
On 11/1/21 17:10, Steven Schveighoffer wrote:
> A shared member is a sharable member of the class. It does not put the
> item in global storage.
>
> There are some... odd rules.
>
> struct S
> {
> static int a; // TLS
> shared static int b; // shared data storage
> shared int c; // local variable, but its type is shared(int)
> immutable int d; // local immutable variable, settable only in
> constructor
> immutable int e = 5; // stored in data segment, not per instance!
> __gshared int f; // stored in global segment, typed as int, not
> shared(int)
> }
Thanks for the detailed explanation! I think this mixing of types and
storage classes makes a very unfortunate combination:
```
import std;
int i = 0;
shared int j = 0;
struct S {
int i = 0;
shared int j = 0;
}
S s;
void main() {
i = 1;
j = 1;
s.i = 1;
s.j = 1;
spawn(&f);
}
void f() {
assert(i == 0); // Expected
assert(j == 1); // Expected
assert(s.i == 0); // Expected
assert(s.j == 0); // Wait, what?
}
```
I agree that once you know the inner workings it makes sense, but a
naïve approach might suggest that `s.j` would be... well, shared, just
like `j`.
More information about the Digitalmars-d-learn
mailing list