Project

General

Profile

Actions

Feature #9145

open

Queue#pop(true) return nil if empty instead of raising ThreadError

Added by jsc (Justin Collins) almost 11 years ago. Updated almost 7 years ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:58545]

Description

I propose the non-blocking form of Queue#pop behave like Array#pop and return nil when empty.

Current behavior is to raise a ThreadError, with a message indicating the queue is empty.

For example:

q = Queue.new
begin
loop do
next_item = q.pop(true)
end
rescue ThreadError

queue is empty...or maybe something bad happened

end

Instead, this could be

q = Queue.new
while next_item = q.pop(true)
end

Alternatively, raise an exception that is a subclass of ThreadError with a more specific name, such as "QueueEmpty". This would be a small improvement while remaining compatible with existing code.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0