Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
Thread::Queue#close
() -> self
close
thread_sync.rb
View on GitHub ↗
# File tmp/rubies/ruby-4.1.0-preview1/thread_sync.rb, line 123
def close
Primitive.cstmt! %{
if (!queue_closed_p(self)) {
FL_SET_RAW(self, QUEUE_CLOSED);
wakeup_all(&queue_ptr(self)->waitq);
}
return self;
}
end
Closes the queue. A closed queue cannot be re-opened.
After the call to close completes, the following are true:
-
closed?will return true -
closewill 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 aThreadError.
ClosedQueueError is inherited from StopIteration, so that you can break loop block.
Example:
Example
q = Thread::Queue.new Thread.new{ while e = q.deq # wait for nil to break loop # ... end } q.close