public
Last active

A proof-of-concept limited-thread-count worker example thingy.

  • Download Gist
thread-test.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
require 'threadpuddle'
puddle = ThreadPuddle.new 10
@array = (1..100).to_a
 
while @array.size > 0
# this shift is not synced because '@array'
# is only accessed from the one thread
item = @array.shift
#print "popped item #{item.inspect} at time #{Time.now.strftime '%H:%M:%S.%6N'} Puddle size is #{puddle.size}\n"
puddle.spawn(item) do |i|
# simulate some long-running, not entirely
# uniformly timed operation
sleep(rand/2+0.01)
p i
end
end
puddle.join

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.