Skip to content

Instantly share code, notes, and snippets.

@TwP
Created July 16, 2009 20:45
Show Gist options
  • Save TwP/148676 to your computer and use it in GitHub Desktop.
Save TwP/148676 to your computer and use it in GitHub Desktop.
require 'servolux'
require 'thread'
q = Queue.new
ary = []
3.times do
piper = Servolux::Piper.new 'rw'
piper.parent {
ary << piper
}
piper.child {
begin
loop {
job = piper.gets
result = execute_work_function(job)
piper.puts result
}
ensure
exit!
end
}
end
ary.map! do |piper|
Thread.new(piper) {|piper|
loop {
piper.puts q.pop
result = piper.gets
}
}
end
100000000.times {|n| q << n}
# wait on all threads here so the main program does not exit and kill your
# parent process.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment