# A loop in a thread... very greedy

**URL:** <https://www.ruby-forum.com/t/a-loop-in-a-thread-very-greedy/124618>\
**Category:** Ruby\
**Created:** [December 31, 2007, 4:52am UTC](https://www.ruby-forum.com/t/a-loop-in-a-thread-very-greedy/124618 "2007-12-31T04:52:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jazzhammer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9a140/32.png) [@jazzhammer](https://www.ruby-forum.com/u/jazzhammer)\
**Post date:** [December 31, 2007, 4:52am UTC](https://www.ruby-forum.com/t/a-loop-in-a-thread-very-greedy/124618/1 "2007-12-31T04:52:42Z")

</div>

need a class(we’ll call it a ‘simulator’ since that’s what i’m  
attempting to write) to run a loop.  
need that loop to stop when asked to, such as when the user that started  
it clicks a button to stop it.

i write java that does this sort of thing all the time.  
HOWEVER, when i try it in ruby, only bad things happen.

check this out:

_ **start r.rb** _  
class Simulator  
attr\_accessor :running  
attr\_accessor :sim\_thread  
def start  
running = true;  
puts “starting simulator…”  
sim\_thread= Thread.new(){  
puts “sim thread started”  
while(running)  
300000.times do |x|  
x+=1  
end  
puts ‘did a buncha stuff a simulator might do’  
Thread.pass  
end  
puts “sim thread stopped”  
}  
sim\_thread.join  
sim\_thread  
end  
def stop  
puts “stopping simulator…”  
running = false  
end  
end  
Thread.abort\_on\_exception = true  
simulator = Simulator.new  
thread = simulator.start  
puts “started simulator, now we stop it…”  
simulator.stop  
_ **end r.rb** _

i get no exceptions in the thread.  
the output:  
starting simulator…  
sim thread started  
did a buncha stuff a simulator might do  
did a buncha stuff a simulator might do  
did a buncha stuff a simulator might do

this line never executes:

puts “started simulator, now we stop it…”

why does ruby not leave the thread lone to do its thing,  
and do other things ?

instead, it seems to get stuck in the while loop, as if i didn’t put it  
in a separate thread.

any help much appreciated, thanks.

---

<div class="post-metadata">

**Author:** ![jazzhammer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9a140/32.png) [@jazzhammer](https://www.ruby-forum.com/u/jazzhammer)\
**Post date:** [December 31, 2007, 4:59am UTC](https://www.ruby-forum.com/t/a-loop-in-a-thread-very-greedy/124618/2 "2007-12-31T04:59:39Z")

</div>

On Dec 30, 2007, at 10:52 PM, Abraham Tio wrote:

> ```
> sim_thread.join
> 
> ```

You’re explicitly asking the main thread to let  
the sim\_thread run to completion. Don’t join  
with another thread if you don’t want to wait  
for it to exit.

Gary W.

---

<div class="post-metadata">

**Author:** ![jazzhammer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9a140/32.png) [@jazzhammer](https://www.ruby-forum.com/u/jazzhammer)\
**Post date:** [December 31, 2007, 5:45am UTC](https://www.ruby-forum.com/t/a-loop-in-a-thread-very-greedy/124618/3 "2007-12-31T05:45:16Z")

</div>

> why does ruby not leave the thread lone to do its thing,  
> and do other things ?  
> it does … usually, but since you’re explicitly joining it, it’ll wait  
> in start method untill thread finishes

> instead, it seems to get stuck in the while loop, as if i didn’t put it  
> in a separate thread.  
> You did, and then you turned multi threaded application into single  
> threaded one

> any help much appreciated, thanks.  
> remove join. read documentation (as java user you should be used to it)

---

<div class="post-metadata">

**Author:** ![jazzhammer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9a140/32.png) [@jazzhammer](https://www.ruby-forum.com/u/jazzhammer)\
**Post date:** [December 31, 2007, 6:12am UTC](https://www.ruby-forum.com/t/a-loop-in-a-thread-very-greedy/124618/4 "2007-12-31T06:12:08Z")

</div>

On Dec 30, 2007, at 7:52 PM, Abraham Tio wrote:

> Thread.abort\_on\_exception = true  
> simulator = Simulator.new  
> thread = simulator.start  
> puts “started simulator, now we stop it…”  
> simulator.stop

Others have pointed out not to use join. Of course, once you remove  
join, the start and stop methods are invoked one right after another,  
so you must put a sleep(n) between the two to observe the child thread  
running independent of the main thread.

---

<div class="post-metadata">

**Author:** ![jazzhammer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9a140/32.png) [@jazzhammer](https://www.ruby-forum.com/u/jazzhammer)\
**Post date:** [December 31, 2007, 6:46am UTC](https://www.ruby-forum.com/t/a-loop-in-a-thread-very-greedy/124618/5 "2007-12-31T06:46:00Z")

</div>

On Dec 30, 2007, at 7:52 PM, Abraham Tio wrote:

> _ **start r.rb** _  
> class Simulator  
> attr\_accessor :running  
> attr\_accessor :sim\_thread  
> def start  
> running = true

```
   self.running = true

```

> ```
> end
> puts "sim thread stopped"
> 
> ```
> 
> }  
> sim\_thread.join  
> sim\_thread  
> end  
> def stop  
> puts “stopping simulator…”  
> running = false

```
   self.running = false

```

> end  
> end  
> Thread.abort\_on\_exception = true  
> simulator = Simulator.new  
> thread = simulator.start  
> puts “started simulator, now we stop it…”  
> simulator.stop  
> _ **end r.rb** _

You have an attr\_accessor :running but you were assigning to a  
running local variable. You need to either use @running = true or  
self.running = true\> When ruby sees ‘running = true’ it decides that  
running is a local variable, so using running = does not set the  
@running ivar via the attr\_acessor but self.running = true does.

Cheers-

- Ezra Z.  
– Founder & Software Architect  
– removed\_email\_address@domain.invalid  
– [EngineYard.com](http://EngineYard.com)

---

<div class="post-metadata">

**Author:** ![jazzhammer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9a140/32.png) [@jazzhammer](https://www.ruby-forum.com/u/jazzhammer)\
**Post date:** [December 31, 2007, 9:53am UTC](https://www.ruby-forum.com/t/a-loop-in-a-thread-very-greedy/124618/6 "2007-12-31T09:53:20Z")

</div>

Abraham Tio wrote:

> need a class(we’ll call it a ‘simulator’ since that’s what i’m  
> attempting to write) to run a loop.  
> need that loop to stop when asked to, such as when the user that started  
> it clicks a button to stop it.
> 
> i write java that does this sort of thing all the time.  
> HOWEVER, when i try it in ruby, only bad things happen.
> 
> check this out:

here a woking code

class Simulator  
attr\_accessor :running  
attr\_accessor :sim\_thread  
def start  
@running = true; #instance var  
puts “starting simulator…”  
@sim\_thread= Thread.new(){ #instance var  
puts “sim thread started”  
while(@running)  
300000.times do |x|  
x+=1  
end  
puts ‘did a buncha stuff a simulator might do’  
Thread.pass  
end  
puts “sim thread stopped”  
}  
# @sim\_thread.join no join  
@sim\_thread  
end  
def stop  
puts “stopping simulator…”  
@running = false  
end  
end  
Thread.abort\_on\_exception = true  
simulator = Simulator.new  
thread = simulator.start  
puts “started simulator, now we stop it…”  
simulator.stop

# wait simulator end

simulator.sim\_thread.join

Enjoy!
