class Queue

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

Public Class Methods

new() Show source
static VALUE
rb_queue_initialize(VALUE self)
{
    RSTRUCT_SET(self, QUEUE_QUE, ary_buf_new());
    RSTRUCT_SET(self, QUEUE_WAITERS, ary_buf_new());
    return self;
}

Creates a new queue instance.

Public Instance Methods

<<(p1)

Alias for push.

Alias for: push
clear() Show source
static VALUE
rb_queue_clear(VALUE self)
{
    rb_ary_clear(GET_QUEUE_QUE(self));
    return self;
}

Removes all objects from the queue.

deq(*args)

Alias for pop.

Alias for: pop
empty? Show source
static VALUE
rb_queue_empty_p(VALUE self)
{
    return queue_length(self) == 0 ? Qtrue : Qfalse;
}

Returns true if the queue is empty.

enq(p1)

Alias for push.

Alias for: push
length Show source
size
static VALUE
rb_queue_length(VALUE self)
{
    unsigned long len = queue_length(self);
    return ULONG2NUM(len);
}

Returns the length of the queue.

Also aliased as: size
num_waiting() Show source
static VALUE
rb_queue_num_waiting(VALUE self)
{
    unsigned long len = queue_num_waiting(self);
    return ULONG2NUM(len);
}

Returns the number of threads waiting on the queue.

pop(non_block=false) Show source
deq(non_block=false)
shift(non_block=false)
static VALUE
rb_queue_pop(int argc, VALUE *argv, VALUE self)
{
    int should_block = queue_pop_should_block(argc, argv);
    return queue_do_pop(self, should_block);
}

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.

Also aliased as: deq, shift
push(object) Show source
enq(object)
<<(object)
static VALUE
rb_queue_push(VALUE self, VALUE obj)
{
    return queue_do_push(self, obj);
}

Pushes the given object to the queue.

Also aliased as: enq, <<
shift(*args)

Alias for pop.

Alias for: pop
size()

Alias for length.

Alias for: length

Ruby Core © 1993–2017 Yukihiro Matsumoto
Licensed under the Ruby License.
Ruby Standard Library © contributors
Licensed under their own licenses.