www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Can't create "shared" Mutex with "-preview=nosharedaccess"

reply Gavin Ray <ray.gavin97 gmail.com> writes:
Example below:

- https://ldc.godbolt.org/z/aj7xdbb76

The following error is thrown, which I am not sure I understand 
since it's part of the initialization/ctor code for the shared 
resource:

```d
<source>(27): Error: direct access to shared `new 
shared(ReadWriteMutex)(Policy.PREFER_WRITERS)` is not allowed, 
see `core.atomic`
```

```d
import core.sync.rwmutex : ReadWriteMutex;

// The below works:
//
// 
https://github.com/dlang/dmd/commit/63cb064d3f62317159f999477c9dc15a5ca7d632#diff-652a0df83aadb85a63bc618ed724c49f6b7d4ea45d679ace8c7c90ea9c032537
struct Child
{
     this(int) shared {}
}

struct Parent
{
     shared Child ch;
     this(int i) shared
     {
         ch = shared Child(i);
     }
}

// But this doesn't?
class DiskManager
{
     shared ReadWriteMutex dbIOMutex;

     this() shared
     {
         this.dbIOMutex = new shared ReadWriteMutex();
     }
}
```
Aug 28 2022
parent reply Simen =?UTF-8?B?S2rDpnLDpXM=?= <simen.kjaras gmail.com> writes:
On Sunday, 28 August 2022 at 21:15:59 UTC, Gavin Ray wrote:
 ```
 class DiskManager
 {
     shared ReadWriteMutex dbIOMutex;

     this() shared
     {
         this.dbIOMutex = new shared ReadWriteMutex();
     }
 }
 ```
The solution for now is to cast away shared on the LHS (and not use shared on the rhs): ``` this() shared { cast()this.dbIOMutex = new ReadWriteMutex(); } ``` However, the Child/Parent structs in your code, and https://issues.dlang.org/show_bug.cgi?id=21793 seem to indicate that the cast should not be necessary. I would guess it's a detail about how classes are reference types that messes things up. -- Simen
Aug 29 2022
parent Gavin Ray <ray.gavin97 gmail.com> writes:
Thanks Simen, I was really scratching my head on that one.

I think it may be a bug though -- that doesn't seem intentional 
to me, ha.
Aug 29 2022