Class: ThreadLimiter

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

Overview

Fork threads like Thread.fork, but limit the number of concurrently running threads.

ThreadLimiter isn’t a thread pool. Each fork really starts a new thread.

Instance Method Summary collapse

Constructor Details

#initialize(limit = -1)) ⇒ ThreadLimiter

Initialize the ThreadLimter. The optional parameter limit is the maximum number of concurrently running threads. Set limit to -1 or 0 to fork threads without limiting the number of concurrently running threads.



11
12
13
14
15
16
17
# File 'lib/threadlimiter/threadlimiter.rb', line 11

def initialize(limit=-1)
  @limit	= limit	# The maximum number of concurrently running threads.
  @running	= 0	# The number of currently running threads.

  @mutex	= Mutex.new
  @cv		= ConditionVariable.new
end

Instance Method Details

#fork(*args, &block) ⇒ Object

Fork a thread. The given block is run within the thread. It behaves like Thread.fork(). In fact, it invokes Thread.fork() and returns its result. The list of arguments is passed to Thread.fork().



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/threadlimiter/threadlimiter.rb', line 25

def fork(*args, &block)
  if @limit <= 0
    Thread.fork(*args, &block)
  else
    @mutex.synchronize do
      while @running >= @limit
        @cv.wait(@mutex)
      end

      @running	+= 1
    end

    Thread.fork do
      begin
        res	= yield(*args)
      ensure
        @mutex.synchronize do
          @running	-= 1
        end

        @cv.signal	if @limit > 0
      end

      res
    end
  end
end