Acess variable that was set by thread

Steven Schveighoffer schveiguy at gmail.com
Mon Aug 8 14:29:43 UTC 2022


On 8/8/22 10:12 AM, ag0aep6g wrote:
> On Monday, 8 August 2022 at 13:31:04 UTC, Steven Schveighoffer wrote:
>> On 8/8/22 6:17 AM, ag0aep6g wrote:
> [...]
>>> Never ever use `__gshared` ever. It's a glaring safety hole. Use 
>>> `shared` instead.
>>
>> If you are interfacing with C, you need __gshared. But yeah, you 
>> should use shared in this case.
> 
> A quick test suggests that `extern(C) extern shared` works fine.
> 
> As far as I can tell, `__gshared` is only ever ok-ish when you want to 
> access a shared C variable in a single-threaded program. And then you're 
> still setting yourself up for failure if you later add more threads.
> 
> So, never ever use `__gshared` (in multi-threaded code) ever.

C has no notion of shared, so it's not the right type. Putting `shared` 
on it is kind of lying, and can lead to trouble. Better to be explicit 
about what it is.

I'm not saying you should use `__gshared` liberally, or for cases where 
you are using this only in D. But to say you should *never* use it is 
incorrect.

-Steve


More information about the Digitalmars-d-learn mailing list