Sha256: 297b61aa187a31e82d46dcf0acee2a7ef2ba2ffe4a6a30f9eb64f5d2c8e40a7c
Contents?: true
Size: 721 Bytes
Versions: 5
Compression:
Stored size: 721 Bytes
Contents
# http://spin.atomicobject.com/2014/07/07/ruby-queue-pop-timeout/ class QueueWithTimeout def initialize @mutex = Mutex.new @queue = [] @recieved = ConditionVariable.new end def push(item) @mutex.synchronize do @queue << item @recieved.signal end end def <<(item) push(item) end def pop(non_block = false) pop_with_timeout(non_block ? 0 : nil) end def pop_with_timeout(timeout = nil) @mutex.synchronize do if @queue.empty? @recieved.wait(@mutex, timeout) if timeout != 0 # if we're still empty after the timeout, raise exception raise ThreadError, 'queue empty' if @queue.empty? end @queue.shift end end end
Version data entries
5 entries across 5 versions & 1 rubygems