Class: ThreadsWait
- Inherits:
-
Object
- Object
- ThreadsWait
- Extended by:
- Exception2MessageMapper
- Defined in:
- lib/thwait.rb
Overview
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
th = ThreadsWait.new(thread1,...)
th.next_wait # next one to be done
Constant Summary
- RCS_ID =
'-$Id: thwait.rb,v 1.3 1998/06/26 03:19:34 keiju Exp keiju $-'
Constants included from Exception2MessageMapper
Instance Attribute Summary (collapse)
-
- (Object) threads
readonly
Returns the array of threads that have not terminated yet.
Class Method Summary (collapse)
-
+ (Object) all_waits(*threads)
Waits until all specified threads have terminated.
Instance Method Summary (collapse)
-
- (Object) all_waits
Waits until all of the specified threads are terminated.
-
- (Boolean) empty?
Returns true if there are no threads in the pool still running.
-
- (Boolean) finished?
Returns true if any thread has terminated and is ready to be collected.
-
- (ThreadsWait) initialize(*threads)
constructor
Creates a ThreadsWait object, specifying the threads to wait on.
-
- (Object) join(*threads)
Waits for specified threads to terminate, and returns when one of the threads terminated.
-
- (Object) join_nowait(*threads)
Specifies the threads that this object will wait for, but does not actually wait.
-
- (Object) next_wait(nonblock = nil)
Waits until any of the specified threads has terminated, and returns the one that does.
Methods included from Exception2MessageMapper
Fail, Raise, Raise, bind, def_e2message, def_e2message, def_exception, def_exception, e2mm_message, extend_object, fail
Constructor Details
- (ThreadsWait) initialize(*threads)
Creates a ThreadsWait object, specifying the threads to wait on. Non-blocking.
53 54 55 56 57 |
# File 'lib/thwait.rb', line 53 def initialize(*threads) @threads = [] @wait_queue = Queue.new join_nowait(*threads) unless threads.empty? end |
Instance Attribute Details
- (Object) threads (readonly)
Returns the array of threads that have not terminated yet.
60 61 62 |
# File 'lib/thwait.rb', line 60 def threads @threads end |
Class Method Details
+ (Object) all_waits(*threads)
Waits until all specified threads have terminated. If a block is provided, it is executed for each thread as they terminate.
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/thwait.rb', line 38 def ThreadsWait.all_waits(*threads) # :yield: thread tw = ThreadsWait.new(*threads) if block_given? tw.all_waits do |th| yield th end else tw.all_waits end end |
Instance Method Details
- (Object) all_waits
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.
126 127 128 129 130 131 |
# File 'lib/thwait.rb', line 126 def all_waits until @threads.empty? th = next_wait yield th if block_given? end end |
- (Boolean) empty?
Returns true if there are no threads in the pool still running.
65 66 67 |
# File 'lib/thwait.rb', line 65 def empty? @threads.empty? end |
- (Boolean) finished?
Returns true if any thread has terminated and is ready to be collected.
72 73 74 |
# File 'lib/thwait.rb', line 72 def finished? !@wait_queue.empty? end |
- (Object) join(*threads)
Waits for specified threads to terminate, and returns when one of the threads terminated.
80 81 82 83 |
# File 'lib/thwait.rb', line 80 def join(*threads) join_nowait(*threads) next_wait end |
- (Object) join_nowait(*threads)
Specifies the threads that this object will wait for, but does not actually wait.
89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/thwait.rb', line 89 def join_nowait(*threads) threads.flatten! @threads.concat threads for th in threads Thread.start(th) do |t| begin t.join ensure @wait_queue.push t end end end end |
- (Object) next_wait(nonblock = nil)
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.
110 111 112 113 114 115 116 117 118 |
# File 'lib/thwait.rb', line 110 def next_wait(nonblock = nil) ThreadsWait.fail ErrNoWaitingThread if @threads.empty? begin @threads.delete(th = @wait_queue.pop(nonblock)) th rescue ThreadError ThreadsWait.fail ErrNoFinishedThread end end |