Created
April 28, 2012 05:45
-
-
Save headius/2516355 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# normal thread.rb Queue | |
q = Queue.new | |
wr = nil | |
# use block to ensure the reference goes away | |
tap do | |
obj = Object.new | |
wr = WeakRef.new(obj, q) | |
# reference is live at this point | |
puts wr.__getobj__ | |
# queue is empty at this point | |
puts q.pop(true) rescue puts 'nothing in queue' | |
end | |
# run GC, presumably triggering finalizer insert into queue | |
GC.start | |
# reference is dead at this point | |
puts wr.__getobj__ rescue nil | |
# queue now contains the weakref object | |
puts q.pop(true).class |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment