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?

Methods

<<   clear   deq   empty?   enq   length   new   num_waiting   pop   push   shift   size  

Public Class methods

Creates a new queue.

[Source]

     # 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

Public Instance methods

<<(obj)

Alias for push

Removes all objects from the queue.

[Source]

     # File lib/thread.rb, line 300
300:   def clear
301:     @que.clear
302:   end
deq(non_block=false)

Alias for pop

Returns true is the queue is empty.

[Source]

     # File lib/thread.rb, line 293
293:   def empty?
294:     @que.empty?
295:   end
enq(obj)

Alias for push

Returns the length of the queue.

[Source]

     # File lib/thread.rb, line 307
307:   def length
308:     @que.length
309:   end

Returns the number of threads waiting on the queue.

[Source]

     # 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.

[Source]

     # 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.

[Source]

     # 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
shift(non_block=false)

Alias for pop

Alias of length.

[Source]

     # File lib/thread.rb, line 314
314:   def size
315:     length
316:   end

[Validate]