a question with [Thread]

I

Im still

[Note: parts of this message were removed to make it a legal post.]

hi
when im running a code like this

a = 0
Thread.start{
while true
puts "#{a+=1}"
end
}

This will be terminated automatically but wont keep on looping as expected.
And by my test, the running result will always terminated while a = 41.means
after putting "41" it stops.

Im doing this in windows vista. I dont know if its OS related issue or not.
or maybe im doing wrong ? anyone can tell me?
 
7

7stud --

Im said:
hi
when im running a code like this

a = 0
Thread.start{
while true
puts "#{a+=1}"
end
}

This will be terminated automatically but wont keep on looping as
expected.

When a Ruby program terminates, all threads are killed, regardless of
their states. (pickaxe2 p. 137)
And by my test, the running result will always terminated while a =
41.means
after putting "41" it stops.

It seems strange to me that the thread would get killed at the same
point every time.
Im doing this in windows vista. I dont know if its OS related issue or
not.
or maybe im doing wrong ? anyone can tell me?

...you can wait for a particular thread to finish by calling that
thread's Thread#join method. The calling thread [e.g. your main
program] will block until the given thread is finished...If you don't
want to block forever, you can give join a timeout parameter...
(pickaxe2 p. 137)
 
A

Alex

My guess is that since you're not joining the thread to the main
thread, it just terminates after the main thread exits. Try this:

a =3D 0
thread =3D Thread.new do
while true
puts "#{a+=3D1}"
end
end
thread.join



Alex
 
I

Im still

[Note: parts of this message were removed to make it a legal post.]

thanks Alex. I solved it that way. :D
 

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

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top