Skip to content

Instantly share code, notes, and snippets.

Created May 26, 2012 12:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/2793811 to your computer and use it in GitHub Desktop.
Save anonymous/2793811 to your computer and use it in GitHub Desktop.
ruby.rb
#!/usr/bin/env ruby
require 'thread'
wait_mutex = Mutex.new
wait_condition = ConditionVariable.new
class Pool
def initialize()
@queue = Queue.new
end
def add(obj)
@queue.enq(obj)
end
def process()
5.times do
Thread.new do
begin
obj = @queue.deq(false) rescue nil
if obj
yield obj
end
ensure
wait_mutex.synchronize { wait_condition.signal() }
end
end
end
end
end
pool = Pool.new
pool.add('my work')
loop do
pool.process() do |work|
puts work
end
wait_mutex.synchronize { wait_condition.wait(wait_mutex) }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment