Skip to content

Instantly share code, notes, and snippets.

@gmcinnes
Created July 3, 2012 19:55
Show Gist options
  • Select an option

  • Save gmcinnes/3042536 to your computer and use it in GitHub Desktop.

Select an option

Save gmcinnes/3042536 to your computer and use it in GitHub Desktop.
Processes
require 'thread'
collection = (0..100).to_a
t = collection.length - 1
cpus = 4
segment = t / cpus
queue = Queue.new
producer = Thread.new do
t.times do
queue << collection.pop
end
end
consumer = Thread.new do
# Once for each CPU
cpus.times do
values = []
# Segment = collection length / # cpus
segment.times do
# We pull from the queue, which will block until the
# producer puts something in the queue
# hasn't put anything in the queue
values << queue.pop
end
# Start a new process based on the list of things that are in values
pid = Process.fork do
puts values.join(",")
sleep 10
exit
end
# Don't wait for the process to return, but we better make sure in that
# case the process is writing out something somewhere. That shouldn't be
# hard. Use File to do that
Process.detach(0)
end
end
consumer.join
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment