From: Michael Baker Date: 2010-12-18T16:03:09+09:00 Subject: Re: background threads --0016e6dab4dff894ea0497a9e08c Content-Type: text/plain; charset=ISO-8859-1 I'm not sure that I understand your question, but there are few things I see that might be problematic. One problem is that you pass the variable that you want to change, aFlag, to the Thread constructor. This creates a local copy of aFlag for the thread. When you change aFlag to true inside of setSignal, it's not changing the copy of aFlag used by your thread. If you want to access the same aFlag that you set to false at the top of your run method, you would do it as follows: self.aFlag = false Thread.new() { |arg| while !self.aFlag # do something end } If you do this, you'll need to be sure to prevent your two threads from accessing the aFlag variable at the same time with a mutex lock. Inside of setSignal you say c.aFlag = true. I'm assuming you mean self.aFlag = true. In display you call sleep without a parameter. This will cause your main thread to sleep forever after you start the second thread, meaning that your program will stop there. I hope this helps! On Fri, Dec 17, 2010 at 7:56 PM, Venkat Akkineni < venkatram.akkineni@gmail.com> wrote: > 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/. > > --0016e6dab4dff894ea0497a9e08c--