Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Thread#join
# A reminder that join conceptually combines the stacks of the parent and child threads.
# If you consider that the stack grows downard, the child stack will sit below the parent
# stack. See output below.
def main?
Thread.current == Thread.main
end
def name
if main?
"main"
else
"thread"
end
end
def shout(msg)
puts "#{name}: #{msg}"
end
def a
b
shout "done with a"
end
def b
c
shout "done with b"
end
def c
if main?
Thread.new do
sleep 3
a
shout "done with thread"
end.join
end
shout "done with c"
end
a
=begin
thread: done with c
thread: done with b
thread: done with a
thread: done with thread
main: done with c
main: done with b
main: done with a
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment