Thread, locking TcpSocket from each other?

I got a question regarding threads and the function gets. Lets say I got
two threads running simultaneously. Both are reading from the same
TCPSocket. Will the threads be able to read from the same TCPSocket at
the same time, or will the first thread lock it from the other.

Example code

[code]
def user_idle_time(connection, name, conn)
  Timeout::timeout(3.5) do
    connection.send("whois #{name} #{name} \r\n", 0)
    while server_response = connection.gets
    if(server_response.split(" ")[1].to_i == 317)
      return conn["Factotum"].seconds_to_hms(server_response.split("
")[4].to_i)
    else
      @cached_messages << server_response
    end
    end
  end
  rescue Timeout::Error
  return "Unknown"
  end
[/code]

···

--
Posted via http://www.ruby-forum.com/.

Walle Wallen wrote in post #975044:

I got a question regarding threads and the function gets. Lets say I got
two threads running simultaneously. Both are reading from the same
TCPSocket. Will the threads be able to read from the same TCPSocket at
the same time, or will the first thread lock it from the other.

Don't do this. If there's a lot of data coming in, probably one thread
will get some and another thread get the rest.

Example code

[code]
def user_idle_time(connection, name, conn)
  Timeout::timeout(3.5) do
    connection.send("......."} \r\n", 0)
    while server_response = connection.gets
        ......
    end
  end
  rescue Timeout::Error
  return "Unknown"
  end
[/code]

There's only one thread reading from the socket there. Why do you need
another thread reading from it at the same time?

If the protocol is asynchronous (i.e. the far end can send messages to
you on its own initiative, as well as responses to requests you send)
then you probably want a demultiplexor thread. Then this can talk to
other threads by means of Queues.

Regards,

Brian.

···

--
Posted via http://www.ruby-forum.com/\.