| Class | Queue |
| In: |
lib/thread.rb
|
| Parent: | Object |
This class provides a way to communicate data between threads.
TODO: an example (code or English) would really help here. How do you set up a queue between two threads?
Creates a new queue.
# File lib/thread.rb, line 242
242: def initialize
243: @que = []
244: @waiting = []
245: @que.taint # enable tainted comunication
246: @waiting.taint
247: self.taint
248: end
Removes all objects from the queue.
# File lib/thread.rb, line 300
300: def clear
301: @que.clear
302: end
Returns true is the queue is empty.
# File lib/thread.rb, line 293
293: def empty?
294: @que.empty?
295: end
Returns the length of the queue.
# File lib/thread.rb, line 307
307: def length
308: @que.length
309: end
Returns the number of threads waiting on the queue.
# File lib/thread.rb, line 321
321: def num_waiting
322: @waiting.size
323: end
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.
# File lib/thread.rb, line 277
277: def pop(non_block=false)
278: while (Thread.critical = true; @que.empty?)
279: raise ThreadError, "queue empty" if non_block
280: @waiting.push Thread.current
281: Thread.stop
282: end
283: @que.shift
284: ensure
285: Thread.critical = false
286: end
Pushes obj to the queue.
# File lib/thread.rb, line 253
253: def push(obj)
254: Thread.critical = true
255: @que.push obj
256: begin
257: t = @waiting.shift
258: t.wakeup if t
259: rescue ThreadError
260: retry
261: ensure
262: Thread.critical = false
263: end
264: begin
265: t.run if t
266: rescue ThreadError
267: end
268: end