If you could make any changes to D, what would they look like?
Paul Backus
snarwin at gmail.com
Mon Oct 25 18:59:51 UTC 2021
On Monday, 25 October 2021 at 18:22:59 UTC, IGotD- wrote:
> On Monday, 25 October 2021 at 18:12:36 UTC, Paul Backus wrote:
>>
>> As far as I know this is not what `shared` does, and not what
>> `shared` is intended to do. By itself, `shared` is just a
>> marker for data that requires synchronization to access (what
>> [the spec][1] calls "shared memory locations"). Whether that
>> synchronization is accomplished using atomic operations or
>> locking is entirely up to the programmer.
>>
>> [1]: https://dlang.org/spec/intro.html
>
> Last time I tried a shared struct I had to cast away the atomic
> operations on about every line. Is this removed in newer
> compiler versions?
I think perhaps you are mistaking the suggestion to use atomic
operations in the compiler's error message for the actual
presence of atomic operations in the code.
```d
shared int x;
void main()
{
x += 1;
// Error: read-modify-write operations are not allowed for
`shared` variables
// Use `core.atomic.atomicOp!"+="(x, 1)` instead
}
```
The compiler requires you to use *some* kind of synchronization
to modify `x`. The error message (perhaps misguidedly) *suggests*
using atomic operations, but they are not actually required--you
could also use a mutex.
If you're using a mutex, you do have to cast away `shared` once
you have locked it, since in general the compiler has no way of
knowing which mutex is associated with which variable.
More information about the Digitalmars-d
mailing list