Created
May 26, 2012 12:42
-
-
Save anonymous/2793811 to your computer and use it in GitHub Desktop.
ruby.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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