Use of mutex in destructors

Steven Schveighoffer schveiguy at yahoo.com
Fri Apr 27 08:59:50 PDT 2012


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


More information about the Digitalmars-d-learn mailing list