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
Removes all objects from the queue.
static VALUE rb_queue_clear(VALUE self) { rb_ary_clear(GET_QUEUE_QUE(self)); return self; }
Returns true
if the queue is empty.
static VALUE rb_queue_empty_p(VALUE self) { return queue_length(self) == 0 ? Qtrue : Qfalse; }
Returns the length of the queue.
static VALUE rb_queue_length(VALUE self) { unsigned long len = queue_length(self); return ULONG2NUM(len); }
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); }
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) { int should_block = queue_pop_should_block(argc, argv); return queue_do_pop(self, should_block); }