Class: Bow::ThreadPool

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_threads = 4) {|_self| ... } ⇒ ThreadPool

Returns a new instance of ThreadPool.

Yields:

  • (_self)

Yield Parameters:



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

def initialize(max_threads = 4)
  @queue = []
  @threads = []
  @history = []
  @max_threads = max_threads
  return unless block_given?
  yield(self)
  run
end

Instance Attribute Details

#max_threadsObject

Returns the value of attribute max_threads.



5
6
7
# File 'lib/bow/thread_pool.rb', line 5

def max_threads
  @max_threads
end

#queueObject

Returns the value of attribute queue.



5
6
7
# File 'lib/bow/thread_pool.rb', line 5

def queue
  @queue
end

Instance Method Details

#add(&task) ⇒ Object



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

def add(&task)
  add_task(task)
end

#from_enumerable(enumerable, &block) ⇒ Object



29
30
31
32
33
34
# File 'lib/bow/thread_pool.rb', line 29

def from_enumerable(enumerable, &block)
  enumerable.each do |params|
    task = build_task(block, params)
    add_task(task)
  end
end

#runObject



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

def run
  @queue.each do |task|
    wait
    run_task(task)
  end
  wait_for_all
end