Bugs: Browse | Submit New | Admin

[#12544] Unblock() method for Queue & SizedQueue

Date:
2007-07-24 14:15
Priority:
3
Submitted By:
Gary McGhee (magpie)
Assigned To:
Akinori MUSHA (knu)
Category:
Threads / Synchronization
State:
Open
Platform:
 
Summary:
Unblock() method for Queue & SizedQueue

Detailed description
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

Add A Comment: Notepad

Please login


Followup

No Followups Have Been Posted

Attached Files:

Name Description Download
No Files Currently Attached

Changes:

No Changes Have Been Made to This Item