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.



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

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.



6
7
8
# File 'lib/zm/utils/thread_pool.rb', line 6

def number
  @number
end

#resultObject (readonly)

Returns the value of attribute result.



6
7
8
# File 'lib/zm/utils/thread_pool.rb', line 6

def result
  @result
end

#sizeObject (readonly)

Returns the value of attribute size.



6
7
8
# File 'lib/zm/utils/thread_pool.rb', line 6

def size
  @size
end

Instance Method Details

#clear!Object



37
38
39
# File 'lib/zm/utils/thread_pool.rb', line 37

def clear!
  @jobs = []
end

#finish!(result, number) ⇒ Object



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

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

#finish?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/zm/utils/thread_pool.rb', line 17

def finish?
  @finish
end

#jobs_sizeObject



28
29
30
# File 'lib/zm/utils/thread_pool.rb', line 28

def jobs_size
  @jobs.size
end

#run!Object

run threads and perform jobs from queue



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

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

#schedule(*args, &block) ⇒ Object

add a job to queue



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

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

#stopObject



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

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