Class: QuickUtils::ThreadRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/quick_utils/thread_runner.rb

Class Method Summary collapse

Class Method Details

.run(dataset, nthreads) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/quick_utils/thread_runner.rb', line 4

def self.run(dataset, nthreads)
  threads = []
  count = dataset.size
  nthreads = 1 if count < nthreads
  bin_size = count / nthreads
  puts "Dividing #{count} items into sizes of #{bin_size} for #{nthreads} threads."
  t = 0
  while t < nthreads do
    subset = dataset[t*bin_size..t*bin_size+bin_size - 1]
    threads << Thread.new(subset) do |cs|
      yield cs
    end
    t = t + 1
  end
  threads.each {|t| t.join}
end