Extended maintenance of Ruby versions 1.8.7 and 1.9.2 ended on July 31, 2014. Read more

In Files

  • thread.rb

Class/Module Index [+]

Quicksearch

Queue

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

Public Class Methods

new() click to toggle source

Creates a new queue.

 
               # File thread.rb, line 266
def initialize
  @que = []
  @waiting = []
  @que.taint          # enable tainted comunication
  @waiting.taint
  self.taint
end
            

Public Instance Methods

<<(obj) click to toggle source

Alias of push

Alias for: push
clear() click to toggle source

Removes all objects from the queue.

 
               # File thread.rb, line 340
def clear
  @que.clear
end
            
deq(non_block=false) click to toggle source

Alias of pop

Alias for: pop
empty?() click to toggle source

Returns true is the queue is empty.

 
               # File thread.rb, line 333
def empty?
  @que.empty?
end
            
enq(obj) click to toggle source

Alias of push

Alias for: push
length() click to toggle source

Returns the length of the queue.

 
               # File thread.rb, line 347
def length
  @que.length
end
            
Also aliased as: size
num_waiting() click to toggle source

Returns the number of threads waiting on the queue.

 
               # File thread.rb, line 359
def num_waiting
  @waiting.size
end
            
pop(non_block=false) click to toggle source

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.

 
               # File thread.rb, line 309
def pop(non_block=false)
  while (Thread.critical = true; @que.empty?)
    raise ThreadError, "queue empty" if non_block
    @waiting.push Thread.current
    Thread.stop
  end
  @que.shift
ensure
  Thread.critical = false
end
            
Also aliased as: shift, deq
push(obj) click to toggle source

Pushes obj to the queue.

 
               # File thread.rb, line 277
def push(obj)
  Thread.critical = true
  @que.push obj
  begin
    t = @waiting.shift
    t.wakeup if t
  rescue ThreadError
    retry
  ensure
    Thread.critical = false
  end
  begin
    t.run if t
  rescue ThreadError
  end
end
            
Also aliased as: <<, enq
shift(non_block=false) click to toggle source

Alias of pop

Alias for: pop
size() click to toggle source

Alias of length.

Alias for: length

Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.

If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.

If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.

If you want to help improve the Ruby documentation, please visit Documenting-ruby.org.

blog comments powered by Disqus