Project

General

Profile

Actions

Feature #17357

open

`Queue#pop` should have a block form for closed queues

Added by marcandre (Marc-Andre Lafortune) 8 months ago. Updated 7 months ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:101171]

Description

It is currently difficult to reliably distinguish a nil value in a queue from the nil that is returned when a Queue is closed:

n = 100_000
result = []
t2 = Thread.new { n.times { Thread.pass }} # to make things less predictable
n.times.count do
  q = Queue.new
  t = Thread.new { q.pop; result << q.closed? }
  q << nil
  q.close
  t.join
end
puts result.count(true) # => some number usually > 9990 and < 10000

To be completely sure, one needs a Mutex or wrap/unwrap nil values.

Queue#pop should offer a surefire way to handle closed queues. I propose that an optional block be called in this case:

q = Queue.new.close
q.pop # => nil
q.pop { :closed }  # => :closed

Proposed PR: https://github.com/ruby/ruby/pull/3830


Related issues

Related to Ruby master - Feature #10600: [PATCH] Queue#closeClosedko1 (Koichi Sasada)Actions
Actions

Also available in: Atom PDF