Skip to content

Instantly share code, notes, and snippets.

@yaauie
Created May 21, 2014 07:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yaauie/7f88d758871244f10414 to your computer and use it in GitHub Desktop.
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.
$ ruby ~/Desktop/sleepy.rb
signals registered.
^CCAUGHT INT!
4
# 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