Deprecate synchronized classes please!

Loara loara at noreply.com
Tue Sep 6 09:09:49 UTC 2022


Even this very simple code
```d
synchronized class A{
   private:
     int a;
   public:
     this() pure {
       a = 1;
     }

     int getA() pure{
       a++;
       return a;
     }
}

int main(){
   A c = new A();
   return 0;
}
```
fails to compile with `dmd v2.100.1`
```
main.d(10): Error: read-modify-write operations are not allowed 
for `shared` variables
main.d(10):        Use `core.atomic.atomicOp!"+="(this.a, 1)` 
instead
```

Now it seems that D is moving from `synchronized` approach (that 
brings a lot of deadlocks threats) to a `shared` approach 
(passing only references to integer variables and access them 
only with atomic operations). Maybe we should finally deprecate 
`synchronized` class and leave only `synchronized` functions 
inside `@system` code since they can't be safe due to deadlock 
risks?


More information about the Digitalmars-d mailing list