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 153
153:   def all_waits
154:     until @threads.empty?
155:       th = next_wait
156:       yield th if block_given?
157:     end
158:   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, and returns when one of the threads terminated.

[Source]

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

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

[Source]

     # File lib/thwait.rb, line 116
116:   def join_nowait(*threads)
117:     threads.flatten!
118:     @threads.concat threads
119:     for th in threads
120:       Thread.start(th) do |t|
121:         begin
122:           t.join
123:         ensure
124:           @wait_queue.push t
125:         end
126:       end
127:     end
128:   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 137
137:   def next_wait(nonblock = nil)
138:     ThreadsWait.fail ErrNoWaitingThread if @threads.empty?
139:     begin
140:       @threads.delete(th = @wait_queue.pop(nonblock))
141:       th
142:     rescue ThreadError
143:       ThreadsWait.fail ErrNoFinishedThread
144:     end
145:   end

[Validate]