Created
August 12, 2014 17:15
-
-
Save cjbottaro/2bfd0e8582d9ee2e083c to your computer and use it in GitHub Desktop.
Keep track of parent thread when spawning new thread
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
class Thread | |
def self.spawn(*args, &block) | |
new(Thread.current, *args) do |*args| | |
Thread.current[:parent] = args.first | |
block.call(*args[1..-1]) | |
end | |
end | |
singleton_class.class_eval{ private :new } | |
end | |
puts "Main: #{Thread.main}" | |
thread1 = Thread.spawn do | |
puts "Thread1: #{Thread.current}" | |
puts "Thread1:parent #{Thread.current[:parent]}" | |
thread2 = Thread.spawn("test") do |s| | |
puts "Thread2(#{s}): #{Thread.current}" | |
puts "Thread2:parent #{Thread.current[:parent]}" | |
end | |
thread2.join | |
end | |
thread1.join |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment