Thread overview
Can't create "shared" Mutex with "-preview=nosharedaccess"
Aug 28, 2022
Gavin Ray
Aug 29, 2022
Simen Kjærås
Aug 29, 2022
Gavin Ray
August 28, 2022

Example below:

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:

<source>(27): Error: direct access to shared `new shared(ReadWriteMutex)(Policy.PREFER_WRITERS)` is not allowed, see `core.atomic`
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();
    }
}
August 29, 2022

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

August 29, 2022

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.