Skip to content

Instantly share code, notes, and snippets.

@cjbottaro
Created August 12, 2014 17:15
Show Gist options
  • Save cjbottaro/2bfd0e8582d9ee2e083c to your computer and use it in GitHub Desktop.
Save cjbottaro/2bfd0e8582d9ee2e083c to your computer and use it in GitHub Desktop.
Keep track of parent thread when spawning new thread
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