Threads not garbage collected ?

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Wed Feb 22 00:20:41 PST 2017


On Wednesday, February 22, 2017 07:58:45 David Nadlinger via Digitalmars-d 
wrote:
> On Wednesday, 22 February 2017 at 07:14:27 UTC, Jonathan M Davis
>
> wrote:
> > In this particular case, the program would probably exit if you
> > did
> >
> > void main()
> > {
> >
> >     {
> >
> >         auto a = new A;
> >
> >     }
> >
> >     import core.memory;
> >     GC.collect();
> >
> > }
>
> I very much doubt so. A Thread object isn't just a handle or
> thread reference, but also encapsulates some of the
> system/druntime state required to make threading work. Executing
> Thread.~this() while a thread was still running would lead to a
> crash.
>
> (The tricky part is to make sure the Thread object becomes
> eligible for collection once a thread has actually finished.)

Well, the OP's code wrapped the Thread object in another class and joined
the thread in its finalizer, so you would think that the Thread would be
joined before its finalizer was called, but thinking further on it, IIRC,
there's no guarantee that the GC wouldn't try and collect the Thread object
before calling the finalizer (since there are no other references to the
Thread). So, I suppose that it could fail because of that. But if that
doesn't happen, it should work, because the wrapper calls join rather than
simply letting the Thread be destroyed.

In any case, I don't know if manually running a collection like I showed
would work or not, but if not, I wouldn't expect calling join in a finalizer
to work in general.

- Jonathan M Davis



More information about the Digitalmars-d mailing list