Forum: Ruby Thread Question

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Drew O. (Guest)
on 2007-02-06 20:50
As posted earlier, I'm working on a simple chat server. However, I seem
to be confused as to how I'm supposed to spawn a new thread. Let's
suppose I want to create a new Thread where an instance of Foo listens
for messages on a socket and echos them. Is this correct? I have similar
code in my server that I'm working on, but it is not functioning
properly. It seems that my infinite while loops are blocking the rest of
the code after the '#keep doing more stuff here' section. Confusing...

mysocket = TCPSocket('',1000);{|socket|}
# keep doing more stuff here

class Foo
  def initialize(socket)
    @socket = socket

  def listen
    while true
      message = socket.gets
      puts message
Brian C. (Guest)
on 2007-02-06 23:05
(Received via mailing list)
On Wed, Feb 07, 2007 at 03:50:35AM +0900, Drew O. wrote:
> # keep doing more stuff here
I think you forgot to call 'accept' in a loop, which returns a new
for each connection, and then start a new thread for each one.

For a concrete, working example see
and scroll down to where it says "class Pop3Server"

For general information on threads see


This topic is locked and can not be replied to.