In Files

  • .ext/thread/thread.c

Class/Module Index [+]

Quicksearch
toggle debugging

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

Public Class Methods

new() click to toggle source

Creates a new queue instance.

 
               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;
}
            

Public Instance Methods

<<(p1) click to toggle source

Alias for push.

Alias for: push
clear() click to toggle source

Removes all objects from the queue.

 
               static VALUE
rb_queue_clear(VALUE self)
{
    rb_ary_clear(GET_QUEUE_QUE(self));
    return self;
}
            
deq(*args) click to toggle source

Alias for pop.

Alias for: pop
empty? click to toggle source

Returns true if the queue is empty.

 
               static VALUE
rb_queue_empty_p(VALUE self)
{
    return queue_length(self) == 0 ? Qtrue : Qfalse;
}
            
enq(p1) click to toggle source

Alias for push.

Alias for: push
length click to toggle source
size

Returns the length of the queue.

 
               static VALUE
rb_queue_length(VALUE self)
{
    unsigned long len = queue_length(self);
    return ULONG2NUM(len);
}
            
Also aliased as: size
num_waiting() click to toggle source

Returns the number of threads waiting on the queue.

 
               static VALUE
rb_queue_num_waiting(VALUE self)
{
    unsigned long len = queue_num_waiting(self);
    return ULONG2NUM(len);
}
            
pop(non_block=false) click to toggle source
deq(non_block=false)
shift(non_block=false)

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.

 
               static VALUE
rb_queue_pop(int argc, VALUE *argv, VALUE self)
{
    VALUE should_block = queue_pop_should_block(argc, argv);
    return queue_do_pop(self, should_block);
}
            
Also aliased as: deq, shift
push(object) click to toggle source
enq(object)
<<(object)

Pushes the given object to the queue.

 
               static VALUE
rb_queue_push(VALUE self, VALUE obj)
{
    return queue_do_push(self, obj);
}
            
Also aliased as: enq, <<
shift(*args) click to toggle source

Alias for pop.

Alias for: pop
size() click to toggle source

Alias for length.

Alias for: length