m_condition.mutex cannot be used in shared method ?

Sean Kelly via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Oct 20 10:37:21 PDT 2014


On Monday, 20 October 2014 at 09:53:23 UTC, Marco Leise wrote:
>
> Thank you for that honest response. The situation is really
> bizarre. I just tried to create a shared worker thread and
> there is no ctor in Thread that creates a shared instance.
>
> Is a shared constructor even meaningful? [1]

If we want to try for having thread-local memory pools then yes.


> If yes, what do we need it for?

See above.  Though there are other problems that will probably
prevent this anyway (immutable being implicitly shared, for one).


> Can't we otherwise just implicitly and safely cast to shared
> _after_ the constructor ran when we write `new 
> shared(Foo)(...)`?

Yep.


> Casting away shared is not @safe. Since this is normal to
> do in synchronized blocks, I figure the whole core.Thread and
> core.sync.xxx family are @system functionality ?

Yes.  Though I really don't like feeling that casts are necessary
in general.  If I have to cast in order to do normal work then
there's probably something wrong with the type system.  Though
I'll note that I also use "mutable" in C++ for what I feel are
completely justifiable reasons (like on a contained Mutex so I
can lock/unlock some region of code in a const method), and D has
been firmly established in opposition to logical const.  Mutexes
are actually a special case in D because they bypass normal type
checking thanks to the way synchronized blocks work, and I'm sure
we could do something similar for shared, but it feels wrong.  I
kind of hope that someone will show me that casting away shared
isn't necessary, kind of like how Monads are a clever response to
immutability in Haskell.


> [1]
> (Note that I created a PR for DMD that disables shared
> destruction:
> https://github.com/D-Programming-Language/dmd/pull/4072)

With all the recent work on the GC, we really really need to
start tracking which thread owns a given non-shared object so it
can be finalized properly.  This may mean having the process of
casting away shared make the executing thread the new owner of
the object.


More information about the Digitalmars-d-learn mailing list