Threads and TCP/IP
Jerry Feldman
gaf at blu.org
Mon Nov 21 17:36:00 EST 2005
On Monday 21 November 2005 4:50 pm, Cole Tuininga wrote:
> I've been out of network programming for a while here and was having a
> random musing that I thought somebody here might be able to answer.
>
> Let's say you have a tcp/ip socket in a given multi-threaded program.
> And let's further say that you have one thread specifically for reading,
> and one thread specifically for writing.
>
> Is it legitimate to have the two threads doing their respective
> operation on the socket simultaneously? In other words, can you
> simultaneously read and write a tcp/ip socket? Or would the threads
> need to do some locking themselves to make sure this doesn't happen?
I have done this many times before. Many times the way I set it up is that I
have a reader thread reading from the socket and placing a message on a
queue with 1 or more writer threads writing.
The main issue to be concerned with is the thread-safeness of library
functions. Normally system calls (like send(2) and recv(2) are threadsafe).
--
Jerry Feldman <gaf at blu.org>
Boston Linux and Unix user group
http://www.blu.org PGP key id:C5061EA9
PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9
More information about the gnhlug-discuss
mailing list