# Threading from a loop

**URL:** <https://www.ruby-forum.com/t/threading-from-a-loop/119945>\
**Category:** Ruby\
**Created:** [November 12, 2007, 1:52pm UTC](https://www.ruby-forum.com/t/threading-from-a-loop/119945 "2007-11-12T13:52:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nickpoole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/54ee81/32.png) [@nickpoole](https://www.ruby-forum.com/u/nickpoole)\
**Post date:** [November 12, 2007, 1:52pm UTC](https://www.ruby-forum.com/t/threading-from-a-loop/119945/1 "2007-11-12T13:52:57Z")

</div>

Hi,

My problem is that I want a function to start as a new thread every time  
it’s called, hence allowing the loop to continue and spawn a thread for  
each iteration of the loop. Currently my loop waits for the function to  
end before it continues. If anyone can anyone tell me what I’m missing,  
or even refer me to some good documentation, I’d be grateful.

My current code is as follows:

cl.add\_message\_callback do |m|  
if m.type != :error  
#break off  
newreplythread = Thread.new(cl, m) {  
#function call is here  
}  
end  
}

(Maybe I’m mistaking the loop for something else, but it’s behaviour  
apears the same)

Thanks

NickPoole

---

<div class="post-metadata">

**Author:** ![nickpoole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/54ee81/32.png) [@nickpoole](https://www.ruby-forum.com/u/nickpoole)\
**Post date:** [November 12, 2007, 8:23pm UTC](https://www.ruby-forum.com/t/threading-from-a-loop/119945/2 "2007-11-12T20:23:28Z")

</div>

> My current code is as follows:
> 
> cl.add\_message\_callback do |m|  
> if m.type != :error  
> #break off  
> newreplythread = Thread.new(cl, m) {  
> #function call is here  
> }  
> end  
> }
> 
> (Maybe I’m mistaking the loop for something else, but it’s behaviour  
> apears the same)
> 
> Thanks
> 
> NickPoole

Code looks good to me unless its never being called.  
[http://www.rubycentral.com/pickaxe/tut\_threads.html](http://www.rubycentral.com/pickaxe/tut_threads.html)

---

<div class="post-metadata">

**Author:** ![nickpoole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/54ee81/32.png) [@nickpoole](https://www.ruby-forum.com/u/nickpoole)\
**Post date:** [November 12, 2007, 11:12pm UTC](https://www.ruby-forum.com/t/threading-from-a-loop/119945/3 "2007-11-12T23:12:30Z")

</div>

Nicholas Poole wrote:

> Hi,
> 
> My problem is that I want a function to start as a new thread every time  
> it’s called, hence allowing the loop to continue and spawn a thread for  
> each iteration of the loop. Currently my loop waits for the function to  
> end before it continues. If anyone can anyone tell me what I’m missing,  
> or even refer me to some good documentation, I’d be grateful.
> 
> My current code is as follows:
> 
> cl.add\_message\_callback do |m|  
> if m.type != :error  
> #break off  
> newreplythread = Thread.new(cl, m) {  
> #function call is here  
> }  
> end  
> }
> 
> (Maybe I’m mistaking the loop for something else, but it’s behaviour  
> apears the same)

Try this:

def greet  
puts ‘hello’  
end

threads = []

5.times do |i|  
threads \<\< Thread.new do  
sleep(rand() \* 10)  
puts “thread#{i}”  
greet  
puts  
end  
puts “loop#{i}”  
end  
puts

threads.each{|thr| thr.join}

–output:–  
loop0  
loop1  
loop2  
loop3  
loop4 #Note that all loop iterations are completed

thread3  
hello

thread0  
hello

thread4  
hello

thread1  
hello

thread2  
hello
