From: Pat Maddox Date: 2006-04-23T21:03:12+09:00 Subject: Re: How do threads and join work? On 4/23/06, Ashley Moran wrote: > > On Apr 23, 2006, at 12:33 pm, Pat Maddox wrote: > > > 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. > > > > 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." > > Sorry - missed your final point because I forgot to scroll down. In > future I won't write replies first thing Sunday morning :) > > You can never join on your thread because it can only be terminated > (cleanly) by setting the value of running to false > > I don't see what's wrong with this (going back to your original code): > > running = true > t = Thread.new do > print "Thread started\n" > while(running); end > print "Thread finished\n" > end > > do_work > > # Use one or: other of these depending on whether t is immortal > running = false > #t.join > #puts "After join" > > puts "Program finished" > > Maybe I have misunderstood whether you want to be in control of a > thread that will run forever, or whether your while loop is just to > simulate a long-running process? > > Ashley > Well this is all because I need to interact with a remote server that sends asyncrhonous messages. I suppose the most common server that's similar is an IRC server. You connect to the server, and then just wait while it pumps out messages to you. The way I've implemented this in other languages is to create a new thread which simply has an infinite loop (checking a control condition, such as @connected) that reads data from a socket. So in cases like this, the read call would block until some data comes through the socket. Then it parses the text and the loop continues. I'm trying to do the same thing with Ruby. Pat