class Buckler::ThreadDispatch

Public Class Methods

new() click to toggle source
# File lib/buckler/thread_dispatch.rb, line 6
def initialize
  @lambda_pool = []
  @thread_pool = []
  @max_threads = Etc.nprocessors # The number of CPU cores available to Ruby
end

Public Instance Methods

any_running_threads?() click to toggle source
# File lib/buckler/thread_dispatch.rb, line 16
def any_running_threads?
  @thread_pool.any?{|s| s.status == "run"}
end
perform_and_wait() click to toggle source
# File lib/buckler/thread_dispatch.rb, line 24
def perform_and_wait

  Thread.abort_on_exception = true

  start_time = Time.now

  @lambda_pool.lazy.each do |λ|
    while running_thread_count >= @max_threads
      verbose "Sleeping due to worker limit. #{running_thread_count} currently running."
      GC.start
      sleep 0.2
    end
    @thread_pool << Thread.new(&λ)
  end

  verbose "All workers spawned, waiting for workers to finish"
  while any_running_threads? do
    sleep 0.2
  end

  return (Time.now - start_time).round(2)

end
queue(λ) click to toggle source
# File lib/buckler/thread_dispatch.rb, line 12
def queue(λ)
  @lambda_pool << λ
end
running_thread_count() click to toggle source
# File lib/buckler/thread_dispatch.rb, line 20
def running_thread_count
  @thread_pool.select{|s| s.status == "run"}.count
end