Class: XThreads::XThread
- Inherits:
-
Object
- Object
- XThreads::XThread
- Defined in:
- lib/xthreads.rb
Instance Attribute Summary collapse
-
#result ⇒ Object
readonly
Returns the value of attribute result.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
Instance Method Summary collapse
-
#initialize(name, args, options = {}, &blk) ⇒ XThread
constructor
A new instance of XThread.
- #kill ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
- #thread ⇒ Object
Constructor Details
#initialize(name, args, options = {}, &blk) ⇒ XThread
Returns a new instance of XThread.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/xthreads.rb', line 15 def initialize(name, args, ={}, &blk) opts = {interval: 4, loop: true}.merge() @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 |
Instance Attribute Details
#result ⇒ Object (readonly)
Returns the value of attribute result.
13 14 15 |
# File 'lib/xthreads.rb', line 13 def result @result end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
13 14 15 |
# File 'lib/xthreads.rb', line 13 def state @state end |
Instance Method Details
#kill ⇒ Object
64 65 66 67 68 |
# File 'lib/xthreads.rb', line 64 def kill puts 'XThread killed' @thread.kill @state = :dead end |
#start ⇒ Object
52 53 54 55 56 57 |
# File 'lib/xthreads.rb', line 52 def start puts "#{@name} starting ..." @state = :start @thread.send @loop == true ? :join : :run end |
#stop ⇒ Object
59 60 61 62 |
# File 'lib/xthreads.rb', line 59 def stop puts "'#{@name}' stopping ..." @state = :stop end |
#thread ⇒ Object
70 71 72 |
# File 'lib/xthreads.rb', line 70 def thread @thread end |