instance method Thread::Queue#deq

pop(non_block = false) -> object[permalink][rdoc][edit]
shift(non_block = false) -> object
deq(non_block = false) -> object

キューからひとつ値を取り出します。キューが空の時、呼出元のスレッドは停止します。

[PARAM] non_block:
true を与えると、キューが空の時に例外 ThreadError が発生します。


require 'thread'

q = Queue.new

th1 = Thread.start do
  while resource = q.pop
    puts resource
  end
end

[:resource1, :resource2, :resource3, nil].each { |r|
  q.push(r)
}

th1.join
例: nonblock = true

require 'thread'

q = Queue.new

th1 = Thread.start do
  while resource = q.pop
    puts resource
  end
end

[:resource1, :resource2, :resource3, nil].each { |r|
  q.push(r)
}

begin
  th1.join
  q.pop(true)
rescue => e
  p e
end

# => resource1
# resource2
# resource3
# => #<ThreadError: queue empty>
# => "queue empty"