class Temporal::ThreadPool
Constants
- EXIT_SYMBOL
Attributes
size[R]
Public Class Methods
new(size)
click to toggle source
# File lib/temporal/thread_pool.rb, line 12 def initialize(size) @size = size @queue = Queue.new @mutex = Mutex.new @availability = ConditionVariable.new @available_threads = size @pool = Array.new(size) do |i| Thread.new { poll } end end
Public Instance Methods
schedule(&block)
click to toggle source
# File lib/temporal/thread_pool.rb, line 31 def schedule(&block) @mutex.synchronize do @available_threads -= 1 @queue << block end end
shutdown()
click to toggle source
# File lib/temporal/thread_pool.rb, line 38 def shutdown size.times do schedule { throw EXIT_SYMBOL } end @pool.each(&:join) end
wait_for_available_threads()
click to toggle source
# File lib/temporal/thread_pool.rb, line 23 def wait_for_available_threads @mutex.synchronize do while @available_threads <= 0 @availability.wait(@mutex) end end end
Private Instance Methods
poll()
click to toggle source
# File lib/temporal/thread_pool.rb, line 50 def poll catch(EXIT_SYMBOL) do loop do job = @queue.pop job.call @mutex.synchronize do @available_threads += 1 @availability.signal end end end end