Acess variable that was set by thread

kdevel kdevel at vogtner.de
Mon Aug 8 18:36:48 UTC 2022


On Monday, 8 August 2022 at 17:45:03 UTC, bauss wrote:
> On Monday, 8 August 2022 at 13:55:02 UTC, ag0aep6g wrote:
>>         auto x = s.x;
>> [...]
>
> Your problem is here and not because it was __gshared.
>
> You're copying the value and obviously it can be changed in the 
> meantime, that's common sense.

The value of `x` changes while `x` is being read.

> You shouldn't use it like that. You should access s.x directly 
> instead.

```
//        auto x = s.x;
         assert(s.x == 0 || s.x == -1, to!string(s.x, 16));
```

this replaces one race by three races which even prevents 
spotting the reason for the triggered assertion:

```
$ > ./race
core.exception.AssertError at race.d(40): FFFFFFFFFFFFFFFF
```

> And in the case of shared it can leave the same result if the 
> reading thread locks first then it will read and process the 
> value before it's changed.

???


More information about the Digitalmars-d-learn mailing list