Acess variable that was set by thread

ag0aep6g anonymous at example.com
Mon Aug 8 19:21:28 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.
>
> You shouldn't use it like that. You should access s.x directly 
> instead.

kdevel has already addressed this.

> 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.

You're right that `shared` doesn't fix the race condition. 
Without `-preview=nosharedaccess`, there is no difference at all. 
So you might as well use `shared` ;)

But with `-preview=nosharedaccess`, the code no longer compiles, 
and you're forced to think about how to access the shared data 
safely. Which is good.

So: Never ever use `__gshared`, and always use 
`-preview=nosharedaccess`.


More information about the Digitalmars-d-learn mailing list