It appears that, in Ruby 2.4 and 2.5, threads don't die as soon as you invoke #kill
on them. This code snippet will print Not dead a few times:
thread = Thread.new { loop {} }
thread.kill
puts "Not dead" while thread.alive?
I'd like to block execution of the main thread until the secondary thread is killed. I tried using thread.join.kill
, but of course this blocks the main thread because the thread's loop never terminates.
How can I ensure that a thread is killed before the main thread continues?
join
blocks the calling thread until the thread's been terminated properly. – Chaos