Skip to content

Instantly share code, notes, and snippets.

@ryanstout
Created April 10, 2021 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanstout/118207345fb88cb4494689af3b18638b to your computer and use it in GitHub Desktop.
Save ryanstout/118207345fb88cb4494689af3b18638b to your computer and use it in GitHub Desktop.
fiber_count = 1
ready_scanline = Channel(UInt32 | Nil).new
available_lines = Channel(UInt32).new
finished = Channel(Nil).new
# Fill available
spawn do
fiber_count.times do |i|
available_lines.send(i.to_u32)
end
end
spawn do
1000.times do |row_num|
puts "i: #{row_num}"
working_line = available_lines.receive
ready_scanline.send(working_line)
end
puts "Send finished nils"
fiber_count.times { ready_scanline.send(nil)}
end
fiber_count.times do |fiber_id|
spawn do
loop do
working_line = ready_scanline.receive
if working_line.nil?
puts "Finished #{fiber_id}"
finished.send(nil)
# Done processing
break
else
puts "requeue: #{working_line}"
# At the very end this blocks forever
available_lines.send(working_line)
puts "done requeue: #{working_line}"
end
end
end
end
puts "Block until all are processed"
fiber_count.times {|i| finished.receive ; puts "Done #{i} received" }
puts "Done received"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment