/test.cr Secret
Created
April 10, 2021 19:40
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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