In Files

  • thread.rb

Class/Module Index [+]

Quicksearch

SizedQueue

This class represents queues of specified size capacity. The push operation may be blocked if the capacity is full.

See Queue for an example of how a SizedQueue works.

Public Class Methods

new(max) click to toggle source

Creates a fixed-length queue with a maximum size of max.

 
               # File thread.rb, line 265
def initialize(max)
  raise ArgumentError, "queue size must be positive" unless max > 0
  @max = max
  @enque_cond = ConditionVariable.new
  @num_enqueue_waiting = 0
  super()
end
            

Public Instance Methods

<<(obj) click to toggle source

Alias of push

Alias for: push
deq(*args) click to toggle source

Alias of pop

Alias for: pop
enq(obj) click to toggle source

Alias of push

Alias for: push
max() click to toggle source

Returns the maximum size of the queue.

 
               # File thread.rb, line 276
def max
  @max
end
            
max=(max) click to toggle source

Sets the maximum size of the queue.

 
               # File thread.rb, line 283
def max=(max)
  raise ArgumentError, "queue size must be positive" unless max > 0

  @mutex.synchronize do
    if max <= @max
      @max = max
    else
      diff = max - @max
      @max = max
      diff.times do
        @enque_cond.signal
      end
    end
  end
  max
end
            
num_waiting() click to toggle source

Returns the number of threads waiting on the queue.

 
               # File thread.rb, line 360
def num_waiting
  @num_waiting + @num_enqueue_waiting
end
            
pop(*args) click to toggle source

Retrieves data from the queue and runs a waiting thread, if any.

 
               # File thread.rb, line 337
def pop(*args)
  retval = super
  @mutex.synchronize do
    if @que.length < @max
      @enque_cond.signal
    end
  end
  retval
end
            
Also aliased as: shift, deq
push(obj) click to toggle source

Pushes obj to the queue. If there is no space left in the queue, waits until space becomes available.

 
               # File thread.rb, line 304
def push(obj)
  Thread.handle_interrupt(RuntimeError => :on_blocking) do
    @mutex.synchronize do
      while true
        break if @que.length < @max
        @num_enqueue_waiting += 1
        begin
          @enque_cond.wait @mutex
        ensure
          @num_enqueue_waiting -= 1
        end
      end

      @que.push obj
      @cond.signal
    end
    self
  end
end
            
Also aliased as: <<, enq
shift(*args) click to toggle source

Alias of pop

Alias for: pop

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 see Improve the docs, or visit Documenting-ruby.org.

blog comments powered by Disqus