# thwait.rb - thread synchronization class
# $Release Version: 0.9 $
# by Keiju ISHITSUKA(Nihon Rational Software Co.,Ltd.)
# 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
# ThreadsWait.all_waits(thr1, thr2, ...) do |t|
# STDERR.puts "Thread #{t} has terminated."
# th = ThreadsWait.new(thread1,...)
# th.next_wait # next one to be done
extend Exception2MessageMapper
def_exception("ErrNoWaitingThread", "No threads for waiting.")
def_exception("ErrNoFinishedThread", "No finished threads.")
# Waits until all specified threads have terminated. If a block is provided,
# it is executed for each thread as they terminate.
def ThreadsWait.all_waits(*threads) # :yield: thread
tw = ThreadsWait.new(*threads)
# Creates a ThreadsWait object, specifying the threads to wait on.
join_nowait(*threads) unless threads.empty?
# Returns the array of threads that have not terminated yet.
# Returns +true+ if there are no threads in the pool still running.
# Returns +true+ if any thread has terminated and is ready to be collected.
# Waits for specified threads to terminate, and returns when one of
# the threads terminated.
# Specifies the threads that this object will wait for, but does not actually
def join_nowait(*threads)
# Waits until any of the specified threads has terminated, and returns the one
# If there is no thread to wait, raises +ErrNoWaitingThread+. If +nonblock+
# is true, and there is no terminated thread, raises +ErrNoFinishedThread+.
def next_wait(nonblock = nil)
ThreadsWait.fail ErrNoWaitingThread if @threads.empty?
@threads.delete(th = @wait_queue.pop(nonblock))
ThreadsWait.fail ErrNoFinishedThread
# 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+.
# An alias for ThreadsWait from thwait.rb
# Documentation comments:
# - Source of documentation is evenly split between Nutshell, existing
# comments, and my own rephrasing.
# - I'm not particularly confident that the comments are all exactly correct.