Skip to content

Instantly share code, notes, and snippets.

@trans
Created September 9, 2009 02:15
Show Gist options
  • Select an option

  • Save trans/183406 to your computer and use it in GitHub Desktop.

Select an option

Save trans/183406 to your computer and use it in GitHub Desktop.
Thread-safe Pool Class
# Pool - Thread Safe Object Store
# Copyright (c) 2004 George Moschovitis
#
# TODO: Could use the SizedQueue/Queue?
require 'thread'
require 'monitor'
# Generalized object pool implementation. Implemented as a thread
# safe stack. Exclusive locking is needed both for push and pop.
#
class Pool < Array
include MonitorMixin
def initialize
super
@cv = new_cond()
end
# Add, restore an object to the pool.
#
def push(obj)
synchronize do
super
@cv.signal()
end
end
alias_method :<<, :push
# Obtain an object from the pool.
#
def pop
synchronize do
@cv.wait_while { empty? }
super
end
end
# Obtains an object, passes it to a block for processing
# and restores it to the pool.
#
def obtain
result = nil
begin
obj = pop()
result = yield(obj)
ensure
push(obj)
end
return result
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment