Use of mutex in destructors

Rene Zwanenburg renezwanenburg at gmail.com
Fri Apr 27 09:16:46 PDT 2012


On Friday, 27 April 2012 at 15:59:50 UTC, Steven Schveighoffer 
wrote:
> On Fri, 27 Apr 2012 11:55:02 -0400, Rene Zwanenburg 
> <renezwanenburg at gmail.com> wrote:
>
>> Which reminds me, does the GC actually block all threads while 
>> calling the destructors on garbage? I'm far from an expert on 
>> GC's, but I believe the mark needs to stop the world, but the 
>> sweep can be done concurrently. If the GC thread calls 
>> destructors while the other threads aren't waiting, there 
>> shouldn't be a problem.
>
> AFAIK, this is how it works (world is resumed during sweep).  
> Otherwise, you would have deadlocks all over the place where 
> you used mutexes in dtors!  Given that arbitrary C library 
> calls may lock something somewhere, there is no way to avoid 
> this.
>
> The one thing you *can't* do is allocate GC memory inside a 
> dtor.
>
> -Steve

Great, thanks. That'll save me a lot a trouble. I've run a few 
times in the 'dtor allocating memory' problem, but it's usually 
easy enough to work around. One more question: Is that a 
limitation of the current GC implementation, or something 
intrinsic to garbage collection in general?


More information about the Digitalmars-d-learn mailing list