while(1) and threads

P

Paul

I have many threads that I leave running, doing things in the
background. I end up writing a lot of code like the following - is
there a way to make a thread run, avoiding the while(1) ?

Thanks

Paul

a = Thread.new(){
aa = 0
while(1)
puts "a thread: #{aa}"
aa=aa+1
end
}

b = Thread.new(){
bb = 0
while(1)
puts "b thread: #{bb}"
bb=bb+2
end
}

a.join
 
K

Kirk Haines

I have many threads that I leave running, doing things in the
background. I end up writing a lot of code like the following - is
there a way to make a thread run, avoiding the while(1) ?

Thanks

Paul

a = Thread.new(){
aa = 0
while(1)
puts "a thread: #{aa}"
aa=aa+1
end
}

Do you just want a different looping construct?

a = Thread.new {
aa = 0
loop do
puts "a thread: #{aa}"
aa += 1
end
}


Kirk Haines
 
G

Gennady

Paul said:
I have many threads that I leave running, doing things in the
background. I end up writing a lot of code like the following - is
there a way to make a thread run, avoiding the while(1) ?

If you need another way to make a loop, here it is:

loop do
end

If you want to avoid a loop altogether, then I do not understand what
you mean. After all a "thread" is a "thread of execution" -- execution
stops, the thread stops.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top