Class: ThreadsWait

Inherits:
Object
  • Object
show all
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

Constant Summary collapse

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*threads) ⇒ ThreadsWait

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



80
81
82
83
84
# File 'lib/thwait.rb', line 80

def initialize(*threads)
  @threads = []
  @wait_queue = Queue.new
  join_nowait(*threads) unless threads.empty?
end

Instance Attribute Details

#threadsObject (readonly)

Returns the array of threads in the wait queue.



87
88
89
# File 'lib/thwait.rb', line 87

def threads
  @threads
end

Class Method Details

.all_waits(*threads) ⇒ Object

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



65
66
67
68
69
70
71
72
73
74
# File 'lib/thwait.rb', line 65

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

#all_waitsObject

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.



152
153
154
155
156
157
# File 'lib/thwait.rb', line 152

def all_waits
  until @threads.empty?
    th = next_wait
    yield th if block_given?
  end
end

#empty?Boolean

Returns true if there are no threads to be synchronized.

Returns:

  • (Boolean)


92
93
94
# File 'lib/thwait.rb', line 92

def empty?
  @threads.empty?
end

#finished?Boolean

Returns true if any thread has terminated.

Returns:

  • (Boolean)


99
100
101
# File 'lib/thwait.rb', line 99

def finished?
  !@wait_queue.empty?
end

#join(*threads) ⇒ Object

Waits for specified threads to terminate.



106
107
108
109
# File 'lib/thwait.rb', line 106

def join(*threads)
  join_nowait(*threads)
  next_wait
end

#join_nowait(*threads) ⇒ Object

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



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/thwait.rb', line 115

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

#next_wait(nonblock = nil) ⇒ Object

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.



136
137
138
139
140
141
142
143
144
# File 'lib/thwait.rb', line 136

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