Hello

Here is a class when run starts printing numbers to the console in a new
thread. The   c.setSignal(true) starts the infinite loop. The loop keeps
printing to the console. But the program never goes beyond that
statement and understandably so, therefore "I am here" never gets
printed.

I am trying to start a thread and let it run in the background until I
call a method and set the aFlag to false. I am unable to visualize this
using semaphores and threads. Could somebody help me.

class Example

  attr_accessor :aFlag , :tr



  def run
    self.aFlag = false
    i = 0

    self.tr=Thread.new(self.aFlag) { |aFlag|
      while !aFlag
        puts i

        i=i+1
        sleep 1
      end
    }
  end

  def display
    self.run
    sleep
  end

  def setSignal(signal)
    if signal == true
      display
    else
      c.aFlag=true
    end
  end

end


  c = Example.new
  c.setSignal(true)


puts "I am here"

-- 
Posted via http://www.ruby-forum.com/.