Method: Thread::Queue#close

Defined in:
thread_sync.c

#closeObject

Closes the queue. A closed queue cannot be re-opened.

After the call to close completes, the following are true:

  • closed? will return true

  • close will be ignored.

  • calling enq/push/<< will raise a ClosedQueueError.

  • when empty? is false, calling deq/pop/shift will return an object from the queue as usual.

  • when empty? is true, deq(false) will not suspend the thread and will return nil. deq(true) will raise a ThreadError.

ClosedQueueError is inherited from StopIteration, so that you can break loop block.

Example:

	q = Thread::Queue.new
  Thread.new{
    while e = q.deq # wait for nil to break loop
      # ...
    end
  }
  q.close


987
988
989
990
991
992
993
994
995
996
997
998
999
# File 'thread_sync.c', line 987

static VALUE
rb_queue_close(VALUE self)
{
    struct rb_queue *q = queue_ptr(self);

    if (!queue_closed_p(self)) {
        FL_SET(self, QUEUE_CLOSED);

        wakeup_all(queue_waitq(q));
    }

    return self;
}