Class: XThread::RBQueue
- Inherits:
-
Object
- Object
- XThread::RBQueue
- Defined in:
- lib/xthread.rb
Direct Known Subclasses
Instance Method Summary collapse
-
#clear ⇒ Object
Removes all objects from the queue.
-
#empty? ⇒ Boolean
Returns
true
if the queue is empty. -
#initialize ⇒ RBQueue
constructor
A new instance of RBQueue.
-
#length ⇒ Object
(also: #size)
Returns the length of the queue.
-
#pop(non_block = false) ⇒ Object
(also: #shift, #deq)
Retrieves data from the queue.
-
#push(obj) ⇒ Object
(also: #<<, #enq)
Pushes
obj
to the queue.
Constructor Details
#initialize ⇒ RBQueue
Returns a new instance of RBQueue.
59 60 61 62 63 64 65 |
# File 'lib/xthread.rb', line 59 def initialize @que = [] @que.taint # enable tainted comunication self.taint @mutex = Mutex.new @cond = XThread::ConditionVariable.new end |
Instance Method Details
#clear ⇒ Object
Removes all objects from the queue.
124 125 126 |
# File 'lib/xthread.rb', line 124 def clear @que.clear end |
#empty? ⇒ Boolean
Returns true
if the queue is empty.
117 118 119 |
# File 'lib/xthread.rb', line 117 def empty? @que.empty? end |
#length ⇒ Object Also known as: size
Returns the length of the queue.
131 132 133 |
# File 'lib/xthread.rb', line 131 def length @que.length end |
#pop(non_block = false) ⇒ Object Also known as: shift, deq
Retrieves data from the queue. If the queue is empty, the calling thread is suspended until data is pushed onto the queue. If non_block
is true, the thread isn’t suspended, and an exception is raised.
92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/xthread.rb', line 92 def pop(non_block=false) @mutex.synchronize{ while true if @que.empty? @cond.wait(@mutex) else return @que.shift end end } end |
#push(obj) ⇒ Object Also known as: <<, enq
Pushes obj
to the queue.
70 71 72 73 74 75 |
# File 'lib/xthread.rb', line 70 def push(obj) @mutex.synchronize do @que.push obj @cond.signal end end |