Skip to contentSkip to conversation

is sendto and recvfrom thread safe?

DISCUSSION Lee Chin 2 participants 2 messages Aug 21, 2003

lore
Share & tools
lore

Lee Chin

author
#0 lore raw

Hi,
If I have a global socket file descriptor, can I call sendto and recvfrom on that file descriptor concurrently with out using semaphores around the call to sendto and recvfrom?

What about for TCP sockets?

Thanks
Lee

Signature · 12 lines
__________________________________________________________
Sign-up for your own personalized E-mail at Mail.com
http://www.mail.com/?sr=signup

CareerBuilder.com has over 400,000 jobs. Be smarter about your job search
http://corp.mail.com/careers

-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
#1 lore raw
Quoted 7 lines · On Thu, 21 Aug 2003 01:06:45 -0500 "Lee Chin" <[email protected]> wrote:

Hi,
If I have a global socket file descriptor, can I call sendto and recvfrom on that file descriptor concurrently with out using semaphores around the call to sendto and recvfrom?

What about for TCP sockets?

Thanks
Lee

It depends on your definition of thread safe.
Sendto and recvfrom are for UDP which is a datagram protocol; therefore each call sends and receives a separate data unit. The application protocol has to be atomic to work with UDP anyway. Your application has to deal with out of order, and dropped packets.

If you use TCP sockets with threaded applications you better do application level locking per socket and have enough information in your protocol to describe the length of each operation. TCP is a byte stream, and with multiple threads sending it is possible to "mix the streams". Multiple threads reading will get out of order data.

This is true of all OS's that I know about that support threads (Windows, Solaris, Linux, ...).