こんばんは、ZnZです。

On [Sun, 21 Nov 1999 03:49:20 +0900]
Kazuhiro Nishiyama <nishiyama / mx1.tiki.ne.jp> wrote:
> [ruby-list:14445]の例を実行すると
> warning: Thread::join is obsolete; use Thread#join instead
> とでるので、Thread.joinをThread.current.joinにしたところ、

他のMLトピックスをみていて、t.joinにすればいいとわかりました(^^;


#!/usr/local/bin/ruby

require 'thread'

class TinyQueue
  def initialize(max=2)
    @max = max
    @full = ConditionVariable.new
    @empty = ConditionVariable.new
    @mutex = Mutex.new
    @q = []
  end

  def count
    @q.size
  end

  def enq(v)
    begin
      @mutex.lock
      @full.wait(@mutex) if count == @max 
      @q.push v
      @empty.signal if count == 1 
    ensure
      @mutex.unlock
    end
  end

  def deq
    begin
      @mutex.lock
      @empty.wait(@mutex) if count == 0 
      v = @q.shift
      @full.signal if count == (@max - 1)
      v
    ensure
      @mutex.unlock
    end
  end

  alias send enq
  alias recv deq
end
  
if __FILE__ == $0
  q = TinyQueue.new(1)

  foods = 'Apple Banana Strawberry Udon Rice Milk'.split

  l = []

  th = Thread.new {
    for obj in foods
      q.send(obj) 
      print "sent ", obj, "\n"
    end
    q.send nil
  }

  l.push th

  th = Thread.new {
    while obj = q.recv
      print "recv ", obj, "\n"
    end
  }
  l.push th

  l.each do |t|
    t.join
  end
end

--- 
ZnZ(ゼット エヌ ゼット)
西山和広(Kazuhiro Nishiyama)
mailto:nishiyama / mx1.tiki.ne.jp