Class: Picdown::MultiThread

Inherits:
Object
  • Object
show all
Defined in:
lib/picdown/multithread.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pool) ⇒ MultiThread

Returns a new instance of MultiThread.



9
10
11
12
13
# File 'lib/picdown/multithread.rb', line 9

def initialize(pool)
  @pool    = pool
  @threads = []
  @queue   = Queue.new
end

Instance Attribute Details

#poolObject (readonly)

Returns the value of attribute pool.



7
8
9
# File 'lib/picdown/multithread.rb', line 7

def pool
  @pool
end

#queueObject (readonly)

Returns the value of attribute queue.



7
8
9
# File 'lib/picdown/multithread.rb', line 7

def queue
  @queue
end

#threadsObject (readonly)

Returns the value of attribute threads.



7
8
9
# File 'lib/picdown/multithread.rb', line 7

def threads
  @threads
end

Instance Method Details

#add(*args, &block) ⇒ Object



15
16
17
# File 'lib/picdown/multithread.rb', line 15

def add(*args, &block)
  queue << [block, args]
end

#invokeObject



19
20
21
22
23
24
25
# File 'lib/picdown/multithread.rb', line 19

def invoke
  1.upto(pool) { threads << spawn_thread }
  threads.each do |t|
    trap('INT') { Thread.kill(t) }
    t.join
  end
end