[Issue 23709] Cannot use synchronized on shared class with -preview=nosharedaccess

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Mar 21 15:15:29 UTC 2023


https://issues.dlang.org/show_bug.cgi?id=23709

RazvanN <razvan.nitu1305 at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |INVALID

--- Comment #7 from RazvanN <razvan.nitu1305 at gmail.com> ---
This is not a bug. The compiler rewrites the code to:

auto tmp = c;
_d_monitorenter(tmp);
try {  body } finally { _d_monitorexit(tmp); }

So the reading of c needs to be atomic. Note that the original example
(synchronized(new shared Class)) currently compiles because creating a shared
instance does not need to be protected by a mutex.

So just write:

import core.atomic;

class Class {}
void main()
{
    shared Class c;
    synchronized(atomicLoad(c)) {}                                              
}

And the code will compile.

Arguably, the compiler could automatically wrap `c` into an `atomicLoad`,
however, that's based on the assumption that druntime is available and that the
users does not employ a different synchronization mechanism.

I will tentatively close this as INVALID, but please reopen if I am missing
something.

--


More information about the Digitalmars-d-bugs mailing list