shared Mutex?

cy via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jun 9 18:19:40 PDT 2016


On Thursday, 9 June 2016 at 20:53:38 UTC, tcak wrote:

>         (cast()mx).lock();

I was told casting away shared when there are still references to 
it is a bad idea. Like, the Mutex object might get corrupted if 
the garbage collector tries to move it while another thread is 
using it.

So thread 1 casts it to unshared, locks it, then allocates 
memory, triggering the GC to move things around. Meanwhile thread 
2 casts it to unshared, tries to lock it, and when it checks the 
locked bit, that area of memory has been replaced with another 
object entirely by the GC.

That seems like a really contrived situation, and maybe not a 
problem at all, if casting away shared doesn't make that memory 
eligible for being moved around. But, color me cautious before 
doing exactly what the FAQ says not to do.

https://dlang.org/faq.html#casting_from_shared


More information about the Digitalmars-d-learn mailing list