Class: ThreadPool::Task

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &callback) ⇒ Task

Returns a new instance of Task.



69
70
71
72
73
74
75
76
77
# File 'lib/threadpool.rb', line 69

def initialize(*args, &callback)
  @args = args
  @callback = callback
  @done = false
  @result = nil
  @exception = nil
  @mutex = Monitor.new
  @cv = @mutex.new_cond
end

Instance Attribute Details

#exceptionObject (readonly)

Returns the value of attribute exception.



67
68
69
# File 'lib/threadpool.rb', line 67

def exception
  @exception
end

#resultObject (readonly)

Returns the value of attribute result.



67
68
69
# File 'lib/threadpool.rb', line 67

def result
  @result
end

Instance Method Details

#executeObject



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/threadpool.rb', line 79

def execute
  begin
    @result = @callback.call(*@args)
  rescue Exception => e
    @exception = e
  STDERR.puts "Error in thread #{Thread.current} - #{e}"
  e.backtrace.each { |element| STDERR.puts(element) }
  end
  @mutex.synchronize do
    @done = true
    @cv.broadcast
  end
end

#joinObject



93
94
95
# File 'lib/threadpool.rb', line 93

def join
  @mutex.synchronize { @cv.wait_until { @done } }
end