If you could make any changes to D, what would they look like?

Paul Backus snarwin at gmail.com
Mon Oct 25 19:52:10 UTC 2021


On Monday, 25 October 2021 at 19:12:52 UTC, Ola Fosheim Grøstad 
wrote:
> On Monday, 25 October 2021 at 18:59:51 UTC, Paul Backus wrote:
>> 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.
>
> 100% misguided, and the type system should not allow it. It 
> cannot be assumed to be safe.

It can be assumed not to cause a data race, which means that an 
atomic operation on a `shared` variable is exactly as safe as the 
corresponding non-atomic operation on a thread-local variable.

>> 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.
>
> And this is where almost all utility of shared is lost. Now you 
> can no longer assume that something that isn't marked as shared 
> is thread local...

[The language spec][1] defines "thread local" as follows:

> *Thread-local memory locations* are accessible from only one 
> thread at a time.

And further clarifies that

> A memory location can be temporarily transferred from shared to 
> local if synchronization is used to prevent any other threads 
> from accessing the memory location during the operation.

Of course, the compiler will not stop you from writing incorrect 
casts in `@system` code, but that's not an issue unique to 
`shared`.

[1]: https://dlang.org/spec/intro.html


More information about the Digitalmars-d mailing list