Threads not garbage collected ?

David Nadlinger via Digitalmars-d digitalmars-d at puremagic.com
Wed Feb 22 00:32:41 PST 2017


On Wednesday, 22 February 2017 at 08:20:41 UTC, Jonathan M Davis 
wrote:
> 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.

True – I was thinking of the subclassing example also shown above.

> 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.

Yes, it wouldn't work in general – mThread isn't guaranteed to be 
around any longer in ~this() for a normal object (although the 
thread lifetime relationship mentioned above complicates the 
analysis here somewhat, and if the thread is never stopped 
somewhere else, it might just as well work by accident).

  — David


More information about the Digitalmars-d mailing list