Threads and TCP/IP
    Kevin D. Clark 
    kevin_d_clark at comcast.net
       
    Mon Nov 21 22:10:01 EST 2005
    
    
  
Cole Tuininga writes:
> 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 assume that you're asking about some non-ancient version of Linux]
Yes, this is legal.  How could you know this?  If you were to dig a
little bit, you could find that the GNU C library conforms to the
POSIX.1 standard, and this standard dictates that both read() and
write() be thread-safe.  So, you don't need to perform any locking in
order to use sockets in this manner.
[I also assume that you are referring to stream and datagram sockets.
I wouldn't be surprised if some esoteric protocol implementations had
some warts in this area.  Caveat programmer.]
I have written an extensive amount of pthreads/socket code that uses
exactly this feature on a couple of platforms.  Because these
platforms conformed to POSIX.1, I was confident that this technique
would work (and, it in fact did work...)
Regards,
--kevin
-- 
GnuPG ID: B280F24E
C/C++/Perl/Network/Linux/etc. guy looking for work.  
Contact for info.
    
    
More information about the gnhlug-discuss
mailing list