Class: Rx::Concurrent::ThreadPool

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

Instance Method Summary collapse

Constructor Details

#initialize(size = Etc.nprocessors) ⇒ ThreadPool

Returns a new instance of ThreadPool.



6
7
8
9
10
# File 'lib/rx/concurrent/thread_pool.rb', line 6

def initialize(size = Etc.nprocessors)
  @pool = []
  @size = size
  @pid = Process.pid
end

Instance Method Details

#restartObject



29
30
31
32
# File 'lib/rx/concurrent/thread_pool.rb', line 29

def restart
  shutdown
  start
end

#shutdownObject



12
13
14
15
16
17
18
# File 'lib/rx/concurrent/thread_pool.rb', line 12

def shutdown
  return unless started?

  queue.close
  pool.map(&:join)
  pool.clear
end

#startObject



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

def start
  return if started?

  @queue = Queue.new
  size.times { pool << Thread.new(&worker) }

  self
end

#started?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/rx/concurrent/thread_pool.rb', line 34

def started?
  pool.map(&:alive?).any?
end

#submit(&block) ⇒ Object



38
39
40
41
42
43
# File 'lib/rx/concurrent/thread_pool.rb', line 38

def submit(&block)
  restart_on_fork if forked?

  return unless started?
  queue << block
end