Class: XThread::RBSizedQueue
Overview
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.
Instance Method Summary collapse
-
#initialize(max) ⇒ RBSizedQueue
constructor
Creates a fixed-length queue with a maximum size of
max
. -
#max ⇒ Object
Returns the maximum size of the queue.
-
#max=(max) ⇒ Object
Sets the maximum size of the queue.
-
#pop(*args) ⇒ Object
(also: #shift, #deq)
Retrieves data from the queue and runs a waiting thread, if any.
-
#push(obj) ⇒ Object
(also: #<<, #enq)
Pushes
obj
to the queue.
Methods inherited from RBQueue
Constructor Details
#initialize(max) ⇒ RBSizedQueue
Creates a fixed-length queue with a maximum size of max
.
158 159 160 161 162 163 |
# File 'lib/xthread.rb', line 158 def initialize(max) raise ArgumentError, "queue size must be positive" unless max > 0 @max = max @cond_wait = ConditionVariable.new super() end |
Instance Method Details
#max ⇒ Object
Returns the maximum size of the queue.
168 169 170 |
# File 'lib/xthread.rb', line 168 def max @max end |
#max=(max) ⇒ Object
Sets the maximum size of the queue.
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/xthread.rb', line 175 def max=(max) diff = nil @mutex.synchronize { if max <= @max @max = max else diff = max - @max @max = max end } if diff diff.times do @cond_wait.signal end end max end |
#pop(*args) ⇒ Object Also known as: shift, deq
Retrieves data from the queue and runs a waiting thread, if any.
222 223 224 225 226 227 228 229 230 |
# File 'lib/xthread.rb', line 222 def pop(*args) retval = super @mutex.synchronize { if @que.length < @max @cond_wait.signal end } retval end |
#push(obj) ⇒ Object Also known as: <<, enq
Pushes obj
to the queue. If there is no space left in the queue, waits until space becomes available.
197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/xthread.rb', line 197 def push(obj) @mutex.synchronize{ while true break if @que.length < @max @cond_wait.wait(@mutex) end @que.push obj @cond.signal } end |