Sha256: 8d850ed67eee00e077783936624be2d1ad4d998ce65ec07f8fa23995280b83f2

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

# https://gist.github.com/dux/466507a5e86cadd2c4714381a1f06cf4

class Thread::Simple
  # Thread::Simple.run do |t|
  #   for foo in bar
  #     t.add { ... }
  #   end
  # end
  def self.run **args
    ts = new
    yield ts
    ts.run
  end

  ###

  attr_accessor :que, :size, :named

  def initialize size: 5, sleep: 0.05
    @sync     = Mutex.new
    @sleep    = sleep
    @size     = size
    @que      = []
    @threds   = []
    @name_val = {}
  end

  def add name = nil, &block
    @sync.synchronize do
      if name
        @que << proc { @name_val[name] = block.call }
      else
        @que << block
      end
    end
  end

  def run endless: false
    @endless = endless

    @size.times do
      @threds << Thread.new do
        task = nil

        while active?
          @sync.synchronize { task = @que.pop }
          task.call if task
          sleep @sleep
        end
      end
    end

    unless @endless
      @threds.each(&:join)
    end
  end

  def stop
    @endless = false
  end

  def [] name
    @name_val[name]
  end

  private

  def active?
    @endless || @que.first
  end
end

###

# pool = Thread::Simple.new

# 1.upto(20) do |i|
#   pool.add i do
#     print '.'
#     time = rand
#     sleep time
#     'Integer: %s (%s - %s)' % [i, pool.que.size, time]
#   end
# end

# Thread.new do
#   sleep 5
#   pool.stop
# end

# pool.run endless: true

# puts

# for key in pool.named.keys.sort
#   puts '%s -> %s' % [key, pool.named[key]]
# end

# puts
# puts pool[10]

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lux-fw-0.6.2 ./lib/overload/thread_simple.rb