On this page
class · Ruby 4.0
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:
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
Class methods 1
Instance methods 14
Ruby version changes 3
-
3.3
-
Thread::Queue#freezenow raisesTypeError. [Bug #17146]
-
-
3.2
-
Thread::Queue#pop(timeout: sec) is added. [Feature #18774]
-
-
3.1
-
Thread::Queue.newnow accepts anEnumerableof initial values. [Feature #17327]
-