Class: FutureProof::ThreadPool

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

Instance Method Summary collapse

Constructor Details

#initialize(size) ⇒ ThreadPool

Returns a new instance of ThreadPool.



5
6
7
8
9
10
# File 'lib/future_proof/thread_pool.rb', line 5

def initialize(size)
  @size    = size
  @threads = []
  @queue   = Queue.new
  @values  = FutureProof::FutureQueue.new
end

Instance Method Details

#finalizeObject



33
34
35
# File 'lib/future_proof/thread_pool.rb', line 33

def finalize
  @size.times { @queue.push :END_OF_WORK }
end

#finalize!Object



52
53
54
55
# File 'lib/future_proof/thread_pool.rb', line 52

def finalize!
  @queue.clear
  finalize
end

#performObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/future_proof/thread_pool.rb', line 16

def perform
  unless @threads.any? { |t| t.alive? }
    @values.start!
    @size.times do
      @threads << Thread.new do
        while job = @queue.pop
          if job == :END_OF_WORK
            break
          else
            @values.push *job[1], &job[0]
          end
        end
      end
    end
  end
end

#submit(*args, &block) ⇒ Object



12
13
14
# File 'lib/future_proof/thread_pool.rb', line 12

def submit(*args, &block)
  @queue.push [block, args]
end

#valuesObject



42
43
44
45
46
# File 'lib/future_proof/thread_pool.rb', line 42

def values
  wait
  @values.stop!
  @values.values
end

#waitObject



37
38
39
40
# File 'lib/future_proof/thread_pool.rb', line 37

def wait
  finalize
  @threads.map &:join
end