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
-- 
Posted via http://www.ruby-forum.com/.