Class: Zm::Utils::ThreadPool

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size) ⇒ ThreadPool

Returns a new instance of ThreadPool.



6
7
8
9
10
11
12
13
# File 'lib/zm/utils/thread_pool.rb', line 6

def initialize(size)
  @finish = false
  @result = nil
  @number = nil
  @size = size
  @jobs = Queue.new
  @pool = init_pool
end

Instance Attribute Details

#numberObject (readonly)

Returns the value of attribute number.



4
5
6
# File 'lib/zm/utils/thread_pool.rb', line 4

def number
  @number
end

#resultObject (readonly)

Returns the value of attribute result.



4
5
6
# File 'lib/zm/utils/thread_pool.rb', line 4

def result
  @result
end

#sizeObject (readonly)

Returns the value of attribute size.



4
5
6
# File 'lib/zm/utils/thread_pool.rb', line 4

def size
  @size
end

Instance Method Details

#clear!Object



35
36
37
# File 'lib/zm/utils/thread_pool.rb', line 35

def clear!
  @jobs = []
end

#finish!(result, number) ⇒ Object



19
20
21
22
23
24
# File 'lib/zm/utils/thread_pool.rb', line 19

def finish!(result, number)
  @finish = true
  @result = result
  @number = number
  @jobs = [[ Proc.new {}, nil]]
end

#finish?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/zm/utils/thread_pool.rb', line 15

def finish?
  @finish
end

#jobs_sizeObject



26
27
28
# File 'lib/zm/utils/thread_pool.rb', line 26

def jobs_size
  @jobs.size
end

#run!Object

run threads and perform jobs from queue



40
41
42
43
44
45
46
# File 'lib/zm/utils/thread_pool.rb', line 40

def run!
  @size.times do
    schedule { throw :exit }
  end
  @pool.map(&:join)
  stop
end

#schedule(*args, &block) ⇒ Object

add a job to queue



31
32
33
# File 'lib/zm/utils/thread_pool.rb', line 31

def schedule(*args, &block)
  @jobs << [block, args]
end

#stopObject



48
49
50
51
52
53
# File 'lib/zm/utils/thread_pool.rb', line 48

def stop
  @jobs.close
  @pool.each(&:exit)
  @pool.clear
  true
end