epoll+xepoll: clarify intent of these concurrency options
[rainbows.git] / lib / rainbows / queue_pool.rb
blob06f7c407f3308d93d0b7363384f87566f60db52b
1 # -*- encoding: binary -*-
2 # :enddoc:
3 require 'thread'
5 # Thread pool class based on pulling off a single Ruby Queue.
6 # This is NOT used for the ThreadPool class, since that class does not
7 # need a userspace Queue.
8 class Rainbows::QueuePool
9   attr_reader :queue
11   def initialize(size = 20)
12     q = Queue.new
13     @threads = (1..size).map do
14       Thread.new do
15         while job = q.shift
16           yield job
17         end
18       end
19     end
20     @queue = q
21   end
23   def quit!
24     @threads.each { |_| @queue << nil }
25     @threads.delete_if do |t|
26       Rainbows.tick
27       t.alive? ? t.join(0.01) : true
28     end until @threads.empty?
29   end
30 end