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.

Methods
#
C
D
E
M
N
P
S
Class Public methods
new(max)

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

# File lib/thread.rb, line 263
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
Instance Public methods
<<(obj)

Alias of push

Alias for: push
clear()

Removes all objects from the queue.

# File lib/thread.rb, line 324
def clear
  super
  @mutex.synchronize do
    @max.times do
      @enque_cond.signal
    end
  end
end
deq(*args)

Alias of pop

Alias for: pop
enq(obj)

Alias of push

Alias for: push
max()

Returns the maximum size of the queue.

# File lib/thread.rb, line 274
def max
  @max
end
max=(max)

Sets the maximum size of the queue.

# File lib/thread.rb, line 281
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()

Returns the number of threads waiting on the queue.

# File lib/thread.rb, line 369
def num_waiting
  @num_waiting + @num_enqueue_waiting
end
pop(*args)

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

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

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

Also aliased as: <<, enq
# File lib/thread.rb, line 302
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
  end
end
shift(*args)

Alias of pop

Alias for: pop