[#9445] thread.rb — m_seki@...

18 messages 2000/03/16
[#9446] Re: thread.rb — matz@... (Yukihiro Matsumoto) 2000/03/17

[#9460] Re: thread.rb — m_seki@... 2000/03/21

[#9462] Re: thread.rb — matz@... (Yukihiro Matsumoto) 2000/03/21

まつもと ゆきひろです

[#11281] Re: thread.rb — Masatoshi SEKI <m_seki@...> 2000/10/22

[#9498] timeout しない timeout — ARIMA Yasuhiro <fit0298@...>

有馬です。

20 messages 2000/03/26
[#9506] Re: timeout しない timeout — matz@... (Yukihiro Matsumoto) 2000/03/27

まつもと ゆきひろです

[#9509] Re: timeout しない timeout — gotoken@... (GOTO Kentaro) 2000/03/27

In message "[ruby-dev:9506] Re: timeout しない timeout"

[ruby-dev:9526] Re: timeout しない timeout

From: ARIMA Yasuhiro <fit0298@...>
Date: 2000-03-29 13:22:40 UTC
List: ruby-dev #9526
有馬です。

gotoken@math.sci.hokudai.ac.jp (GOTO Kentaro) wrote

| sec を yield に渡す必要は感じませんです。

そうですよね。nil or false に限定ですし。
return の結果も評価できないなら return true も不要ですかねぇ。

def timeout(sec)
  return yield  unless sec
  begin
    x = Thread.current
    y = Thread.start {
      sleep sec
      x.raise TimeoutError, "execution expired" if x.status
    }
    yield
  ensure
    Thread.kill y if y.status
  end
end

結果を返すなら、タイムアウトせずに終えたかどうかを返すのでしょうね。

def complete?(sec)
  unless sec
    yield
    return true
  end
  begin
    x = Thread.current
    y = Thread.start {
      sleep sec
      x.raise TimeoutError  if x.alive?
    }
    yield sec
    Thread.kill y if y.alive?
    return true
  rescue  TimeoutError
    return false
  end
end

例外を使わない方がわかりやすいかと思ったのですが、
あまり変わらない。

def complete?(sec)
  unless sec
    yield
    return true
  end
  ret = true
  x = Thread.start {
    yield
  }
  y = Thread.start {
    sleep sec
    Thread.kill x  if x.alive?
    ret = false
  }
  x.join
  Thread.kill y  if y.alive?
  return ret
end

--
 有馬 康弘 <fit0298@fitec.co.jp>

In This Thread