From: Robert Klemme Date: 2008-09-15T17:55:06+09:00 Subject: Re: Garbage collecting threads? 2008/9/14 David Masover : > On Sunday 14 September 2008 15:22:53 Robert Klemme wrote: > >> There is no hook in Ruby that is called when the scope of a variable >> ends. An object without any references can be garbage collected. But a >> finalizer won't work because then the object is gone already. > > What is a "finalizer" in this context? http://www.ruby-doc.org/core/classes/ObjectSpace.html#M006791 >> Also, the >> queue that connects farmer and workers will still have references even >> if the farmer is gone, so the finalizer will never be called... > > Is it possible to have weak references in Ruby? http://www.ruby-doc.org/core/classes/WeakRef.html >> > So, it sounds like you're telling me all the "usual" ways of doing this. >> >> What's wrong with using the usual ways? > > The usual ways still force me to explicitly clean up after my worker threads. > This is irritating, philosophically -- if I wanted to do manual resource > management, I'd write C++, not Ruby. 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). > 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. Note that there are already libraries that abstract such a scenario. Cheers robert -- use.inject do |as, often| as.you_can - without end