Sha256: ae64a74e20e36a08cf9235bec936d39591df50a36ffa908310dcaae676c22ee6
Contents?: true
Size: 709 Bytes
Versions: 5
Compression:
Stored size: 709 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(x) @mutex.synchronize do @queue << x @recieved.signal end end def <<(x) push(x) 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