Skip to content

Instantly share code, notes, and snippets.

@roidrage
Created May 18, 2009 12:43
Show Gist options
  • Save roidrage/113447 to your computer and use it in GitHub Desktop.
Save roidrage/113447 to your computer and use it in GitHub Desktop.
require 'thread'
class ConnectionPool
class Connection
end
def initialize
@mutex = Mutex.new
@pool = {}
end
def current_connection_id
Thread.current.object_id
end
def connection
@mutex.synchronize do
if @pool[current_connection_id]
puts "Found existing connection"
@pool[current_connection_id]
else
if @pool.size == 10
raise "Out of resources"
else
puts "Creating new connection"
@pool[current_connection_id] = Connection.new
puts @pool.size
end
end
end
end
end
pool = ConnectionPool.new
11.times do
Thread.new do
puts pool.connection
pool.connection
end.join
end
require 'thread'
class ConnectionPool
class Connection
end
def initialize
@mutex = Mutex.new
@pool = {}
end
def current_connection_id
Thread.current.object_id
end
def connection
@mutex.synchronize do
if @pool[current_connection_id]
puts "Found existing connection"
@pool[current_connection_id]
else
if @pool.size == 10
raise "Out of resources"
else
puts "Creating new connection"
@pool[current_connection_id] = Connection.new
puts @pool.size
end
end
end
end
def check_in
@pool.delete(current_connection_id)
end
end
pool = ConnectionPool.new
20.times do
Thread.new do
puts pool.connection
pool.connection
pool.check_in
end.join
end
require 'monitor'
class ConnectionPool
class Connection
end
def initialize
@mutex = Monitor.new
@condition = @mutex.new_cond
@pool = {}
end
def current_connection_id
Thread.current.object_id
end
def connection
@mutex.synchronize do
loop do
if @pool[current_connection_id]
return @pool[current_connection_id]
else
if @pool.size == 10
@condition.wait_while {@pool.size == 10}
else
@pool[current_connection_id] = Connection.new
end
end
end
end
end
def check_in
@mutex.synchronize do
@pool.delete(current_connection_id)
@condition.signal
end
end
end
pool = ConnectionPool.new
threads = []
200.times do
threads << Thread.new do
pool.connection
i = 0
while i < 1000000
i+=1
end
pool.check_in
end
end
threads.each {|t| t.join}
f = Fiber.new do
0.upto(100) do |num|
Fiber.yield num
end
end
while (i = f.resume) <= 10
puts i
end
require 'fiber'
class Handler
class Model
def initialize(params)
end
end
def initialize
@worker = Fiber.new do |blk|
loop do
blk.call
@handler.transfer
end
end
@handler = Fiber.new do
loop do
create(nil)
end
end
end
def create(params)
Model.new(params)
blk = lambda {}
@worker.transfer blk
end
def run
@handler.transfer
end
end
Handler.new.run
require 'thread'
class Handler
class Model
def initialize(params)
end
end
def initialize
@queue = Queue.new
@worker = Thread.new do
loop do
while blk = @queue.pop
blk.call
end
end
end
end
def create(params)
Model.new(params)
@queue << lambda do
# do some long-ish calculation
end
end
def run
loop do
create(nil)
end
end
end
Handler.new.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment