Class: Rx::Concurrent::ThreadPool
- Inherits:
-
Object
- Object
- Rx::Concurrent::ThreadPool
- Defined in:
- lib/rx/concurrent/thread_pool.rb
Instance Method Summary collapse
-
#initialize(size = Etc.nprocessors) ⇒ ThreadPool
constructor
A new instance of ThreadPool.
- #restart ⇒ Object
- #shutdown ⇒ Object
- #start ⇒ Object
- #started? ⇒ Boolean
- #submit(&block) ⇒ Object
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
#restart ⇒ Object
29 30 31 32 |
# File 'lib/rx/concurrent/thread_pool.rb', line 29 def restart shutdown start end |
#shutdown ⇒ Object
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 |
#start ⇒ Object
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
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 |