class SoarThreadWorker::ThreadWorker
Attributes
event_handler[RW]
thread[RW]
Public Class Methods
create_worker(event_handler)
click to toggle source
# File lib/soar_thread_worker/thread_worker.rb, line 6 def self.create_worker(event_handler) ThreadWorker.new(event_handler) end
error(message)
click to toggle source
# File lib/soar_thread_worker/thread_worker.rb, line 10 def self.error(message) $stderr.puts "ERROR [ThreadWorker] #{message}" end
new(event_handler: nil)
click to toggle source
# File lib/soar_thread_worker/thread_worker.rb, line 14 def initialize(event_handler: nil) @api_mutex = Mutex.new @stopping = false @event_handler = event_handler end
Public Instance Methods
execute()
click to toggle source
# File lib/soar_thread_worker/thread_worker.rb, line 44 def execute #Inversion of control: override this method to do the work you need. #Return true if after execution the thread should stop, false if it #should continue running false end
running?()
click to toggle source
# File lib/soar_thread_worker/thread_worker.rb, line 20 def running? (not @thread.nil?) and @thread.alive? end
start()
click to toggle source
# File lib/soar_thread_worker/thread_worker.rb, line 24 def start @api_mutex.synchronize { begin return if (running?) @stopping = false create_thread() rescue Exception => e ThreadWorker::error("Exception #{e} in start") raise end } end
stop(immediate: false)
click to toggle source
# File lib/soar_thread_worker/thread_worker.rb, line 37 def stop(immediate: false) @api_mutex.synchronize { @stopping = true @thread.kill if (not @thread.nil?) and immediate } end
Private Instance Methods
be_nice()
click to toggle source
# File lib/soar_thread_worker/thread_worker.rb, line 67 def be_nice sleep(0.01) Thread.pass end
create_thread()
click to toggle source
# File lib/soar_thread_worker/thread_worker.rb, line 60 def create_thread @thread = Thread.new do Thread.abort_on_exception=true run end end
run()
click to toggle source
# File lib/soar_thread_worker/thread_worker.rb, line 53 def run while not @stopping stop if execute be_nice end end