Class: QueueWithTimeout
- Inherits:
-
Object
- Object
- QueueWithTimeout
- Defined in:
- lib/queue_with_timeout.rb
Instance Method Summary collapse
-
#initialize ⇒ QueueWithTimeout
constructor
A new instance of QueueWithTimeout.
- #pop(non_block = false) ⇒ Object
- #pop_with_timeout(timeout = nil) ⇒ Object
- #push(x) ⇒ Object
Constructor Details
#initialize ⇒ QueueWithTimeout
Returns a new instance of QueueWithTimeout.
2 3 4 5 6 |
# File 'lib/queue_with_timeout.rb', line 2 def initialize @mutex = Mutex.new @queue = [] @recieved = ConditionVariable.new end |
Instance Method Details
#pop(non_block = false) ⇒ Object
15 16 17 |
# File 'lib/queue_with_timeout.rb', line 15 def pop(non_block = false) pop_with_timeout(non_block ? 0 : nil) end |
#pop_with_timeout(timeout = nil) ⇒ Object
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/queue_with_timeout.rb', line 19 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 |
#push(x) ⇒ Object
8 9 10 11 12 13 |
# File 'lib/queue_with_timeout.rb', line 8 def push(x) @mutex.synchronize do @queue.push x @recieved.signal end end |