I submit the following as the basis for improvements in the Queue and SizedQueue objects.
When you have worker threads waiting on a SizedQueue (via push or pop), and you want them to shutdown gracefully, the
following method unblock() can be called from any thread, and will cause waiting threads to raise a SizedQueueUnblockError
which they may handle, and perform any shut down logic before terminating normally.
class SizedQueueUnblockError < StandardError; end
SizedQueue.class_eval do
def unblock
Thread.critical = true
begin
while t = @queue_wait.shift do
t.raise(SizedQueueUnblockError.new) unless t == Thread.current
end
while t = @waiting.shift do
t.raise(SizedQueueUnblockError.new) unless t == Thread.current
end
ensure
Thread.critical = false
end
end
end
Regards
magpie |