diff options
Diffstat (limited to 'vendor/supply_drop/lib/supply_drop/thread_pool.rb')
-rw-r--r-- | vendor/supply_drop/lib/supply_drop/thread_pool.rb | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/vendor/supply_drop/lib/supply_drop/thread_pool.rb b/vendor/supply_drop/lib/supply_drop/thread_pool.rb new file mode 100644 index 0000000..082cf4a --- /dev/null +++ b/vendor/supply_drop/lib/supply_drop/thread_pool.rb @@ -0,0 +1,39 @@ +require 'thread' + +module SupplyDrop + class ThreadPool + def initialize(size) + @size = size + @jobs = Queue.new + @retvals = [] + + @pool = Array.new(@size) do |i| + Thread.new do + Thread.current[:id] = i + + catch(:exit) do + loop do + job, args = @jobs.pop + @retvals << job.call(*args) + end + end + end + end + end + + + def schedule(*args, &block) + @jobs << [block, args] + end + + + def shutdown + @size.times do + schedule { throw :exit } + end + + @pool.map(&:join) + @retvals + end + end +end |