end
p “#{Thread.current} restarted”
sleep 5
p “stopping all”
stop_all
sleep 5
p “starting all”
start_all
sleep 5
p “exiting”
On second thought, this is actually just what I need, with a small
modification: only call set_trace_func when the request to stop comes
in, and then call set_trace_func(nil) when threads are restarted.
For example:
th = Thread.new do
loop do
sleep 0.1
puts “alive!”
end
end
And with a little extra work, one can use this to approximate
Thread#backtrace as well as Thread#stop (neither of which exist):
th = Thread.new do
loop do
sleep 0.1
puts “alive!”
end
end
sleep 0.3
puts “stopping”
stopper = proc {|event, file, line, id, binding, classname|
if Thread.current == th
puts caller.join("\n from ")
Thread.stop
end
}
set_trace_func stopper
sleep 1
puts “starting”
set_trace_func nil
th.wakeup
sleep 0.3
alive!
alive!
stopping
thread-stop.rb:6
from thread-stop.rb:6
from thread-stop.rb:4
from thread-stop.rb:3
starting
alive!
alive!
alive!
(This is only approximate because the thread isn’t stopped until after
the interpreter finishes whatever current ruby primitive is evaluating,
but it’s good enough for my purposes.)
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.