Class ThreadsWait
In: lib/thwait.rb
Parent: Object

This class watches for termination of multiple threads. Basic functionality (wait until specified threads have terminated) can be accessed through the class method ThreadsWait::all_waits. Finer control can be gained using instance methods.

Example:

  ThreadsWait.all_wait(thr1, thr2, ...) do |t|
    STDERR.puts "Thread #{t} has terminated."
  end

Methods

all_waits   all_waits   empty?   finished?   join   join_nowait   new   next_wait  

Constants

RCS_ID = '-$Id: thwait.rb,v 1.3 1998/06/26 03:19:34 keiju Exp keiju $-'

Attributes

threads  [R]  Returns the array of threads in the wait queue.

Public Class methods

Waits until all specified threads have terminated. If a block is provided, it is executed for each thread termination.

[Source]

    # File lib/thwait.rb, line 65
65:   def ThreadsWait.all_waits(*threads) # :yield: thread
66:     tw = ThreadsWait.new(*threads)
67:     if block_given?
68:       tw.all_waits do |th|
69:         yield th
70:       end
71:     else
72:       tw.all_waits
73:     end
74:   end

Creates a ThreadsWait object, specifying the threads to wait on. Non-blocking.

[Source]

    # File lib/thwait.rb, line 80
80:   def initialize(*threads)
81:     @threads = []
82:     @wait_queue = Queue.new
83:     join_nowait(*threads) unless threads.empty?
84:   end

Public Instance methods

Waits until all of the specified threads are terminated. If a block is supplied for the method, it is executed for each thread termination.

Raises exceptions in the same manner as next_wait.

[Source]

     # File lib/thwait.rb, line 152
152:   def all_waits
153:     until @threads.empty?
154:       th = next_wait
155:       yield th if block_given?
156:     end
157:   end

Returns true if there are no threads to be synchronized.

[Source]

    # File lib/thwait.rb, line 92
92:   def empty?
93:     @threads.empty?
94:   end

Returns true if any thread has terminated.

[Source]

     # File lib/thwait.rb, line 99
 99:   def finished?
100:     !@wait_queue.empty?
101:   end

Waits for specified threads to terminate.

[Source]

     # File lib/thwait.rb, line 106
106:   def join(*threads)
107:     join_nowait(*threads)
108:     next_wait
109:   end

Specifies the threads that this object will wait for, but does not actually wait.

[Source]

     # File lib/thwait.rb, line 115
115:   def join_nowait(*threads)
116:     threads.flatten!
117:     @threads.concat threads
118:     for th in threads
119:       Thread.start(th) do |t|
120:         begin
121:           t.join
122:         ensure
123:           @wait_queue.push t
124:         end
125:       end
126:     end
127:   end

Waits until any of the specified threads has terminated, and returns the one that does.

If there is no thread to wait, raises ErrNoWaitingThread. If nonblock is true, and there is no terminated thread, raises ErrNoFinishedThread.

[Source]

     # File lib/thwait.rb, line 136
136:   def next_wait(nonblock = nil)
137:     ThreadsWait.fail ErrNoWaitingThread if @threads.empty?
138:     begin
139:       @threads.delete(th = @wait_queue.pop(nonblock))
140:       th
141:     rescue ThreadError
142:       ThreadsWait.fail ErrNoFinishedThread
143:     end
144:   end

[Validate]