class Thread::Queue
The Thread::Queue class implements multi-producer, multi-consumer queues. It is especially useful in threaded programming when information must be exchanged safely between multiple threads. The Thread::Queue class implements all the required locking semantics.
The class implements FIFO (first in, first out) type of queue. In a FIFO queue, the first tasks added are the first retrieved.
Example:
queue = Thread::Queue.new producer = Thread.new do 5.times do |i| sleep rand(i) # simulate expense queue << i puts "#{i} produced" end end consumer = Thread.new do 5.times do |i| value = queue.pop sleep rand(i/2) # simulate expense puts "consumed #{value}" end end consumer.join
Public Instance Methods
() → void
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/thread.rbs, line 1619
def clear: () -> void
Removes all objects from the queue.
() → self
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/thread.rbs, line 1653
def close: () -> self
Closes the queue. A closed queue cannot be re-opened.
After the call to close completes, the following are true:
-
closed?will return true -
closewill be ignored. -
calling enq/push/<< will raise a
ClosedQueueError. -
when
empty?is false, calling deq/pop/shift will return an object from the queue as usual. -
when
empty?is true, deq(false) will not suspend the thread and will return nil. deq(true) will raise aThreadError.
ClosedQueueError is inherited from StopIteration, so that you can break loop block.
Example:
q = Thread::Queue.new Thread.new{ while e = q.deq # wait for nil to break loop # ... end } q.close
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/thread.rbs, line 1661
def closed?: () -> bool
Returns true if the queue is closed.
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/thread.rbs, line 1676
def empty?: () -> bool
Returns true if the queue is empty.
() → bot
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/thread.rbs, line 1690
def freeze: () -> bot
The queue canโt be frozen, so this method raises an exception: Thread::Queue.new.freeze # Raises TypeError (cannot freeze #<Thread::Queue:0xโฆ>)
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/thread.rbs, line 1699
def length: () -> Integer
Returns the length of the queue.
() → Integer
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/thread.rbs, line 1707
def num_waiting: () -> Integer
Returns the number of threads waiting on the queue.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/thread.rbs, line 1722
def pop: (?boolish non_block, ?timeout: _ToF?) -> Elem?
Retrieves data from the queue.
If the queue is empty, the calling thread is suspended until data is pushed onto the queue. If non_block is true, the thread isnโt suspended, and ThreadError is raised.
If timeout seconds have passed and no data is available nil is returned. If timeout is 0 it returns immediately.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/thread.rbs, line 1732
def push: (Elem obj) -> void
Pushes the given object to the queue.