Created
May 21, 2014 07:05
-
-
Save yaauie/7f88d758871244f10414 to your computer and use it in GitHub Desktop.
The following demonstrates that Thread#wakeup can be used in a signal trap to wake up the thread that had control when the signal was raised. I have verified same behaviour in MRI (1.8.7, 1.9.3, 2.0.1, 2.1.1), JRuby (1.7.4, 1.9 mode), Rubinius (2.2.1), but testing signals is hard and I'm unsure how to verify this programatically.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ruby ~/Desktop/sleepy.rb | |
signals registered. | |
^CCAUGHT INT! | |
4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 | |
module Sleepy | |
def self.run(timeout) | |
@sleepy = Thread.new do | |
time = sleep(timeout) | |
puts time.inspect | |
time | |
end | |
%w(INT).each do |sig| | |
trap(sig) do | |
puts "CAUGHT #{sig}!" | |
@sleepy.wakeup | |
end | |
end | |
puts "signals registered." | |
@sleepy.join | |
end | |
end | |
Sleepy.run(20) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment