This class represents queues of specified size capacity. The push operation may be blocked if the capacity is full.
See Queue for an example of how a SizedQueue works.
Returns the maximum size of the queue.
# File thread.rb, line 257 def max @max end
Sets the maximum size of the queue.
# File thread.rb, line 264 def max=(max) diff = nil @mutex.synchronize { if max <= @max @max = max else diff = max - @max @max = max end } if diff diff.times do begin t = @queue_wait.shift t.run if t rescue ThreadError retry end end end max end
Returns the number of threads waiting on the queue.
# File thread.rb, line 361 def num_waiting @waiting.size + @queue_wait.size end
Retrieves data from the queue and runs a waiting thread, if any.
# File thread.rb, line 328 def pop(*args) retval = super t = nil @mutex.synchronize { if @que.length < @max begin t = @queue_wait.shift t.wakeup if t rescue ThreadError retry end end } begin t.run if t rescue ThreadError end retval end
Pushes obj
to the queue. If there is no space left in the
queue, waits until space becomes available.
# File thread.rb, line 291 def push(obj) t = nil @mutex.synchronize{ while true break if @que.length < @max @queue_wait.push Thread.current @mutex.sleep end @que.push obj begin t = @waiting.shift t.wakeup if t rescue ThreadError retry end } begin t.run if t rescue ThreadError end end
Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.
If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.
If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.
If you want to help improve the Ruby documentation, please visit Documenting-ruby.org.