Class: PatchFinder::ThreadPool
- Inherits:
-
Object
- Object
- PatchFinder::ThreadPool
- Defined in:
- lib/patch_finder/core/thread_pool.rb
Instance Attribute Summary collapse
-
#mutex ⇒ Object
Returns the value of attribute mutex.
Instance Method Summary collapse
-
#cleanup ⇒ void
Terminates all threads.
-
#eop? ⇒ boolean
Returns whether there’s anything in the queue left.
-
#initialize(size) ⇒ void
constructor
Initializes the pool.
-
#schedule(*args, &block) ⇒ Object
Adds a job to the queue.
-
#shutdown ⇒ void
Shuts down all the jobs.
Constructor Details
#initialize(size) ⇒ void
Initializes the pool.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/patch_finder/core/thread_pool.rb', line 10 def initialize(size) @size = size @mutex = Mutex.new @jobs = Queue.new @pool = Array.new(@size) do |i| Thread.new do Thread.current[:id] = i catch(:exit) do loop do job, args = @jobs.pop job.call(*args) end end end end end |
Instance Attribute Details
#mutex ⇒ Object
Returns the value of attribute mutex.
4 5 6 |
# File 'lib/patch_finder/core/thread_pool.rb', line 4 def mutex @mutex end |
Instance Method Details
#cleanup ⇒ void
This method returns an undefined value.
Terminates all threads
56 57 58 59 |
# File 'lib/patch_finder/core/thread_pool.rb', line 56 def cleanup @jobs.clear @pool.map(&:kill) end |
#eop? ⇒ boolean
Returns whether there’s anything in the queue left.
49 50 51 |
# File 'lib/patch_finder/core/thread_pool.rb', line 49 def eop? @jobs.empty? end |
#schedule(*args, &block) ⇒ Object
Adds a job to the queue.
31 32 33 |
# File 'lib/patch_finder/core/thread_pool.rb', line 31 def schedule(*args, &block) @jobs << [block, args] end |
#shutdown ⇒ void
This method returns an undefined value.
Shuts down all the jobs.
38 39 40 41 42 43 44 |
# File 'lib/patch_finder/core/thread_pool.rb', line 38 def shutdown @size.times do schedule { throw :exit } end @pool.map(&:join) end |