Sha256: 2656d317cc9e414047a63678ddb59f3332f5e31838ea348f5fb62309d950b3d4

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

# rubocop:disable Style/ClassVars
module Bloodbath
  module Utils
    module Threading
      MAX_ACTIVE_THREADS = 10
      @@threads = []
      @@count = 0

      class << self
        def join_all_threads
          @@threads.each(&:join).tap do
            @@count += @@threads.size
            @@threads = []
          end.map(&:value)
        end
      end

      at_exit do
        Bloodbath::Utils::Verbose.capture("result of threads (exit mode)") do
          Utils::Threading.join_all_threads
        end
      end

      def threading(&block)
        Thread.new(&block).tap do |thread|
          to_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)
        @@threads << thread
      end

      def active_threads
        @@threads
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bloodbath-1.1.1 lib/bloodbath/utils/threading.rb