立石です。

Date: Thu, 30 Sep 1999 15:33:31 +0900
From: Tadashige Morii <m_tada / sx.miracle.ne.jp>
m_tada>  タイマーのようなものはあるのかな?

こういう感じはどうですか?

			Takaaki Tateishi <ttate / jaist.ac.jp>


# Example:
# t = Timer.new
# begin
#   t.start(10)
#   while( loop? )
#     case request
#     when "stop"
#       t.stop; t.reset
#     when "restart"
#       t.reset
#     end
#   end
# rescue TimerError
#   print("timeout")
# end


require 'thread'

class TimerError < StandardError; end

class Timer
  MAXTIME = 0

  def initialize(t=MAXTIME)
    @max_time = t
    @time = 0
    @time_m = Mutex.new
    @thread = nil
  end

  def set(t)
    @max_time = t
  end

  def start(t=@max_time)
    @max_time = t
    cur = Thread.current
    @thread = Thread.new{
      while(true)
	sleep(1)
	if( @time >= @max_time )
	  if( cur.alive? )
	    cur.raise(TimerError,"timeout")
	  end
	end
	@time_m.synchronize{
	  @time += 1
	}
      end
    }
  end

  def reset
    @time_m.synchronize{
      @time = 0
    }
  end

  def stop
    if( @thread and @thread.alive? )
      Thread.kill(@thread)
    end
  end
end