I am trying to write some small timeout code:
t = Thread.new { sleep 3 } # <- The thread that will do stuff.
Thread.new { sleep 2; t.kill; p 'hi!' } # <- The thread that will kill it after two seconds.
t.join
If the first thread completes it's job within two seconds, it will stop, and the main thread will have nothing to do. This will cause the program to exit before the second thread gets to the t.kill
part. But, when I run this code, "hi!"
gets printed out twice. Replacing the p
with puts
fixes it. Why does this happen?