From: Ashley Moran Date: 2006-04-23T21:21:57+09:00 Subject: Re: How do threads and join work? On Apr 23, 2006, at 1:03 pm, Pat Maddox wrote: > 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 > Ahh... I thought this was a newbie question hence patronising intial reply... One possibility is the Queue object in the threads library: require 'thread' messages = Queue.new server = Thread.new do print "Server started\n" 10.times do |i| sleep rand(5) messages << "Message #{i}" end messages << "Disconnect" print "Server finished\n" end client = Thread.new do while true message = messages.pop # blocks here puts "Received: #{message.inspect}" if message == "Disconnect" puts "Disconnecting from server" break end end end puts "Before join" client.join puts "After join" puts "Program finished" This is based on the example in the RDoc There's a load of different ways to synchronise data. My boss has my copy of the pickaxe but they are all cross referenced. Start with Monitor - it's what I used last, although that wasn't for a client- server application but for aggregating data from a group of individual threads. Is this any help? (at last!) Regards Ashley