From: Pat Maddox Date: 2006-04-23T20:35:30+09:00 Subject: Re: How do threads and join work? On 4/23/06, Ashley Moran wrote: > > On Apr 23, 2006, at 11:06 am, Pat Maddox wrote: > > > running = true > > t = Thread.new do > > print "Thread started\n" > > while(running); end > > print "Thread finished\n" > > end > > > > t.join > > puts "After join" > > running = false > > puts "Program finished" > > > > The paragraph I quoted makes me think that it should print "Thread > > started...After join...Thread finished...Program finished" It joins > > the thread and continues processing, setting running to false, which > > causes the thread's loop to end. Also not that it could say "Program > > finished...Thread finished", I don't think there's a way to know for > > sure. > > > Your thread never actually finished, because you joined on it before > you set the value of running to false, so your code blocks at that > point. Right, I mentioned that in my OP. I want to know if there's a way to get join-like behavior - do not terminate the program until that thread has finished executing - without blocking. >You need to swap the last lines like this: > > running = true > t = Thread.new do > print "Thread started\n" > while(running); puts "running = #{running}"; sleep 1; end > puts "running = #{running}"; > print "Thread finished\n" > end > > sleep 5 > > puts "Before join" > running = false > t.join > puts "After join" > puts "Program finished" > > I added two things into the while loop: a sleep so you don't run your > CPU at 100% and a ticker. This gives the output I want, but doesn't actually perform what I want. The ultimate goal here is to basically tell my program "Go start doing this in the background, and I'm going to keep working." Pat