なかだです。

At Tue, 12 Jun 2001 03:56:43 +0900,
Shin'ya Adzumi <adzumi / denpa.org> wrote:
> クラス名はインデックスに使おうかと思っていました。
> 各スレッドごとに一つクラスを作っていたので。
> 定数とか宣言するのが面倒だっただけで実際全然関係ないですね^^;

  うーん、わざわざ Hash にしてる意味がよく分からないのですが、
各スレッドが特定の Queue しか使わないようであれば(私なら)単に
こうします。

q1 = Queue.new
q2 = Queue.new
th1 = Thread.new(q1, q2) {|s, r|
  mail = r.pop; puts mail; s.push('ok')
}
th2 = Thread.new(q2, q1) {|s, r|
  s.push('ok?'); mail = r.pop; puts mail
}
th1.join
th2.join

  あるいは他からは pop できないようにするならこんなんとか。

class Command < Thread
  def initialize
    @queue = Queue.new
    super {Thread.stop; instance_eval &proc}
  end
  def push(x)
    @queue.push(x)
  end
  def pop
    @queue.pop
  end
  private :pop
end

th1 = th2 = nil
th1 = Command.new {
  puts pop; th2.push('ok')
}
th2 = Command.new {
  th1.push('ok?'); puts pop
}
th1.run
th2.run
th1.join
th2.join

> そう言われると確かに。
> ちょっと考えてみたんですが、こういうクラスだとまだ普通(?)でしょ
> うか?

  これだとインスタンス作る必要ないですね。module_function にし
ちゃえば。

-- 
--- 僕の前にBugはない。
--- 僕の後ろにBugはできる。
    中田 伸悦