Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Simple pipeline using Threads and Queues
require "thread"
stage_one = Queue.new
stage_two = Queue.new
stage_three = Queue.new
stage_one_worker = Thread.new do
100.times do |n|
stage_one << n
sleep 1
end
end
stage_two_worker = Thread.new do
while true
n = stage_one.pop
stage_two << n ** 2
end
end
stage_three_worker = Thread.new do
while true
n = stage_two.pop
stage_three << n + 10
end
end
final_stage = Thread.new do
while data = stage_three.pop
puts "final stage data received: #{data}"
end
end
[stage_one_worker, stage_two_worker, stage_three_worker, final_stage].each(&:join)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment