Class: Bake::Multithread::Jobs
- Inherits:
-
Object
- Object
- Bake::Multithread::Jobs
- Defined in:
- lib/multithread/job.rb
Class Method Summary collapse
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.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/multithread/job.rb', line 22 def initialize(jobs, &block) nr_of_threads = [Bake..threads, jobs.length].min @jobs = jobs @threads = [] nr_of_threads.times do @threads << ::Thread.new(Thread.current[:stdout]) do |outStr| Thread.current[:stdout] = outStr begin Jobs.incThread() block.call(self) ensure Jobs.decThread() end end end end |
Class Method Details
.decThread ⇒ Object
15 16 17 |
# File 'lib/multithread/job.rb', line 15 def self.decThread @@semaphore.release end |
.incThread ⇒ Object
12 13 14 |
# File 'lib/multithread/job.rb', line 12 def self.incThread @@semaphore.acquire end |
.init_semaphore ⇒ Object
18 19 20 |
# File 'lib/multithread/job.rb', line 18 def self.init_semaphore @@semaphore = ::Concurrent::MutexSemaphore.new(Bake..threads) end |
Instance Method Details
#failed ⇒ Object
39 40 41 |
# File 'lib/multithread/job.rb', line 39 def failed @failed ||= false end |
#get_next_or_nil ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/multithread/job.rb', line 46 def get_next_or_nil the_next = nil mutex.synchronize { the_next = @jobs.shift } the_next end |
#join ⇒ Object
53 54 55 |
# File 'lib/multithread/job.rb', line 53 def join @threads.each{|t| while not t.join(2) do end} end |
#mutex ⇒ Object
56 57 58 |
# File 'lib/multithread/job.rb', line 56 def mutex @mutex ||= Mutex.new end |
#set_failed ⇒ Object
42 43 44 |
# File 'lib/multithread/job.rb', line 42 def set_failed @failed = true end |