Acess variable that was set by thread

ag0aep6g anonymous at example.com
Mon Aug 8 20:04:32 UTC 2022


On Monday, 8 August 2022 at 19:33:14 UTC, Steven Schveighoffer 
wrote:
> There's nothing clever. If you want to access C globals, you 
> should use `__gshared`, because that's what it is. Using 
> `shared`, isn't going to save you at all.

Yes, using `shared` does save you.

C might not have a `shared` qualifier, but C programmers still 
have to think about thread-safety. Calling a C function or 
accessing a C global always comes with some (possibly implied) 
contract on how to do it safely from multiple threads (the 
contract might be: "don't").

`shared` (with `-preview=nosharedaccess`) forces you to think 
about what the contract is. `__gshared` doesn't.

[...]
> Using `__gshared` to share data with C is as safe as using 
> `-boundscheck=on` and sending the array into C which has no 
> such restrictions.

No it's not. C always being unsafe is true but irrelevant. The 
point is what you can/can't do on the D side.

`-boundscheck=on` - Can't easily mess up on the D side. C side 
can still mess up.
`-boundscheck=off` - Can easily mess up on the D side.
`shared` - Can't easily mess up on the D side. C side can still 
mess up.
`__gshared` - Can easily mess up on the D side.


More information about the Digitalmars-d-learn mailing list