Class Queue
In: lib/thread.rb
Parent: Object

This class provides a way to synchronize communication between threads.

Example:

  require 'thread'

  queue = 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

Methods

<<   clear   deq   empty?   enq   length   new   num_waiting   pop   push   shift   size  

Public Class methods

Creates a new queue.

Public Instance methods

<<(obj)

Alias for push

Removes all objects from the queue.

deq(non_block=false)

Alias for pop

Returns true is the queue is empty.

enq(obj)

Alias for push

Returns the length of the queue.

Returns the number of threads waiting on the queue.

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 an exception is raised.

Pushes obj to the queue.

shift(non_block=false)

Alias for pop

size()

Alias for length

[Validate]

ruby-doc.org is hosted and maintained by James Britt and Happy Camper Studios, a Ruby application development company in Phoenix, Arizona. The site was created in 2002 as part of the Ruby Documentation Project to promote the Ruby language and to help other Ruby hackers.

Documentation content on ruby-doc.org is provided by remarkable members of the Ruby community.

For more information on the Ruby programming language, visit ruby-lang.org.

Want to help improve Ruby's API docs? See Ruby Documentation Guidelines.