Class: Bake::Multithread::Jobs
- Inherits:
-
Object
- Object
- Bake::Multithread::Jobs
- Defined in:
- lib/multithread/job.rb
Instance Method Summary collapse
- #failed ⇒ Object
- #get_next_or_nil ⇒ Object
-
#initialize(jobs, &block) ⇒ Jobs
constructor
A new instance of Jobs.
- #join ⇒ Object
- #mutex ⇒ Object
- #set_failed ⇒ Object
Constructor Details
#initialize(jobs, &block) ⇒ Jobs
Returns a new instance of Jobs.
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/multithread/job.rb', line 9 def initialize(jobs, &block) nr_of_threads = [Bake..threads, jobs.length].min @jobs = jobs @threads = [] nr_of_threads.times do @threads << ::Thread.new do block.call(self) end end end |
Instance Method Details
#failed ⇒ Object
20 21 22 |
# File 'lib/multithread/job.rb', line 20 def failed @failed ||= false end |
#get_next_or_nil ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/multithread/job.rb', line 27 def get_next_or_nil the_next = nil mutex.synchronize { the_next = @jobs.shift } the_next end |
#join ⇒ Object
34 35 36 |
# File 'lib/multithread/job.rb', line 34 def join @threads.each{|t| while not t.join(2) do end} end |
#mutex ⇒ Object
37 38 39 |
# File 'lib/multithread/job.rb', line 37 def mutex @mutex ||= Mutex.new end |
#set_failed ⇒ Object
23 24 25 |
# File 'lib/multithread/job.rb', line 23 def set_failed @failed = true end |