class XThreads::XThread

Attributes

result[R]
state[R]

Public Class Methods

new(name, args, options={}, &blk) click to toggle source
# File lib/xthreads.rb, line 15
def initialize(name, args, options={}, &blk)

  opts = {interval: 4, loop: true}.merge(options)
  @loop = opts[:loop]

  @name = name
  @initialized = true
  @state = :stop

  @thread = Thread.new(*args) { 
    puts "#{name} created\n"
    Thread.current['name'] = name
    loop = true
    while loop == true do

      #puts '@state : ' + @state.inspect

      if @state == :start then
        r = blk.call(args)
        @result = r
        Thread.current[:result] = r
        #@state = :dead
        loop = false if opts[:loop] == false
      else
        #puts 'stopped' unless @initialized == true
        @initialized = false
      end 

      Thread.stop if @state == :stop

      sleep opts[:interval]
    end
  }
  

end

Public Instance Methods

kill() click to toggle source
# File lib/xthreads.rb, line 64
def kill
  puts 'XThread killed'
  @thread.kill
  @state = :dead
end
start() click to toggle source
# File lib/xthreads.rb, line 52
def start

  puts "#{@name} starting ..."
  @state = :start
  @thread.send @loop == true ? :join : :run
end
stop() click to toggle source
# File lib/xthreads.rb, line 59
def stop
  puts "'#{@name}' stopping ..."
  @state = :stop
end
thread() click to toggle source
# File lib/xthreads.rb, line 70
def thread
  @thread
end