lib/bloodbath/utils/threading.rb in bloodbath-1.1.0 vs lib/bloodbath/utils/threading.rb in bloodbath-1.1.1

- old
+ new

@@ -1,6 +1,7 @@ # frozen_string_literal: true + # rubocop:disable Style/ClassVars module Bloodbath module Utils module Threading MAX_ACTIVE_THREADS = 10 @@ -10,23 +11,29 @@ class << self def join_all_threads @@threads.each(&:join).tap do @@count += @@threads.size @@threads = [] - end + end.map(&:value) end end at_exit do - join_all_threads + Bloodbath::Utils::Verbose.capture("result of threads (exit mode)") do + Utils::Threading.join_all_threads + end end - def threading - thread = Thread.new { yield } - to_active_threads(thread) + def threading(&block) + Thread.new(&block).tap do |thread| + to_active_threads(thread) - Utils::Threading.join_all_threads if active_threads.size >= MAX_ACTIVE_THREADS - thread + if active_threads.size >= MAX_ACTIVE_THREADS + Bloodbath::Utils::Verbose.capture("result of threads (reached limit)") do + Utils::Threading.join_all_threads + end + end + end end private def to_active_threads(thread)