From: Brian Candler Date: 2011-01-15T17:27:46+09:00 Subject: Re: Thread, locking TcpSocket from each other? 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/.