From: David Masover Date: 2008-09-16T12:22:21+09:00 Subject: Re: Garbage collecting threads? On Monday 15 September 2008 03:55:06 Robert Klemme wrote: > 2008/9/14 David Masover : > > What is a "finalizer" in this context? > > http://www.ruby-doc.org/core/classes/ObjectSpace.html#M006791 Cool. Can they be lambdas/closures? If so, I think I've got what I need -- it's just going to be very tricky to get right. > > Is it possible to have weak references in Ruby? > > http://www.ruby-doc.org/core/classes/WeakRef.html Hmm. It mentions WeakRef::RefError. While I'm at it -- is it safe to poke at WeakRefs to see if they're still alive? (That is: If the object is gone, will I always get a RefError, or is it ever possible to get something undefined or bad/wrong?) > You have to close file handles as well (even if you use the block form > of File.open you take care of this in a way). The block form of File.open is a good example, though, and it's also an API which is tolerable, in the common case. But even here, it might be useful to wrap it in some sort of garbage-collectable object -- assuming collection run on every single object when the program exits. To be usable, of course, you'd have to force collection to occur when running out of file descriptors, in case one is being held open by an object that hasn't been collected... right? > > It also means that if I'm implementing any kind of library on top of that, I > > have to force all my users to do the same manual cleanup. > > No, because you can make that part of the library. At least you can > built your library in a way that it will invoke a cleanup hook which > you define as empty method for application specific cleanup so users > of the lib do not have to but can add cleanup functionality. Cleanup > of the framework is done automatically of course. The problem is, with the API that it exposes, I don't know when to have the framework cleanup, other than when a particular object has no more strong references to it. I'm not worried about the app-specific cleanup. The idea is: Apps can be built as they always were, just some objects are really actors (object + thread). So, where an app would let an object fall out of scope (and thus be collected), it can also let an actor fall out of scope (and thus be collected, and have the thread closed, too). Thanks for your help. I'm probably going to have to spend a few solid hours (days?) playing with this to be sure, but it looks like I can build this.