DLF September 2023 Monthly Meeting Summary

FeepingCreature feepingcreature at gmail.com
Mon Nov 13 10:11:54 UTC 2023


On Monday, 13 November 2023 at 00:18:35 UTC, Richard (Rikki) 
Andrew Cattermole wrote:
> Part of the problem with shared is that it is completely 
> inverse of what it should be.
>
> It fundamentally does not annotate memory with anything extra 
> that is useful.
>
> At the CPU level there are no guarantees that memory is mapped 
> only to one thread, nor at the language level.
>
> Therefore all memory is shared between threads.
>
> As long as people have this inverted mindset in place and 
> instead of wanting to prove its thread owned shared is going to 
> be a problem for us.
>
> Remove shared, add atomic storage class. Figure out thread 
> owned/shared memory some other day.

Just to once again add my own view, since I don't see it 
represented a lot:

Remove shared or don't, at least *stop making `synchronized` 
imply `shared`.* I don't care about shared, I don't care about 
atomics, I want to use the mutex style of protecting access, 
where I manually think about who exclusively owns what memory. 
But I can't use `synchronized`, because then I have to cast 
*every access to `this`,* which is clearly insane, and as a 
result invariant-using class code is *straight up incorrect,* 
because I have literally no choice but to do this:

```
class Foo
{
   private int[] array;

   invariant { synchronized (this) { assert(this.array.all!(i => i 
 > 0)); } }

   void foo(int i)
   in (i > 0)
   {
     synchronized (this)
     {
       // Note that the invariant on `array` is not actually 
guaranteed here!
       // Some other method in another thread might currently have 
set up a violation
       // for it, but switched out before its out invariant could 
be locked and tested.
       // So we can have a *wrong invariant in this method,* which 
has done absolutely
       // nothing wrong. Very fun!!
       array ~= i;
     }
   }
}
```

The only way to prevent this is `synchronized class`, but even if 
all missing features are implemented, `array` would be `shared`. 
Which is just wrong, as `array` is *privately owned storage of 
the class,* a fact I cannot prove to the compiler but is 
nonetheless true. `synchronized class` brings in the one feature 
I don't want to use and makes it an unavoidable condition of 
*literally writing correct code at all*. So yeah `shared`, I 
didn't *want* to be your enemy but apparently you wanted to be 
mine. `shared` delenda est.



More information about the Digitalmars-d-announce mailing list