Thread, locking TcpSocket from each other?

W

Walle Wallen

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
 
B

Brian Candler

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

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top