Skip to content

Instantly share code, notes, and snippets.

@thejefflarson
Last active December 14, 2015 00:19
Show Gist options
  • Select an option

  • Save thejefflarson/4998523 to your computer and use it in GitHub Desktop.

Select an option

Save thejefflarson/4998523 to your computer and use it in GitHub Desktop.
require 'thread'
class Pool
def initialize(csv, get, set)
@get = get
@set = set
@csv = csv
@queue = Queue.new
@mutex = Mutex.new
end
def <<(work)
@queue << work
end
def wait
@threads = 20.times.map do |id|
Thread.new(&method(:work))
end
@threads.map(&:join)
end
def work
loop do
break if @queue.empty?
row = @queue.shift
res = instance_eval { @get.call(row, @mutex) }
@mutex.synchronize { @csv << instance_eval { @set.call(res) }; }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment