Creates a thread pool that takes a block and run it in parallel with different jobs and returns a list of the results
Ahmed Khaled · @nemoload · over 1 year
Creates a thread pool that takes a block and run it in parallel with different jobs and returns a list of the results
def thread_pool(pool_size: 4, jobs:, &block)threads = []results = []mutex = Mutex.newpool_size.times dothreads << Thread.new dowhile !jobs.empty? dojob = jobs.pop(true)result = block.call(job)mutex.synchronize { results << result }endendendthreads.map(&:join)resultsend