UDP sockets & thread safety, recvfrom() and sendto()

I am writing a UDP server process that uses 2 threads (pthreads). One thread, the one that calls main(), sets up a UDP server socket by making calls such as socket() and bind(). It then goes into an infinite loop which calls recvfrom(). This thread does some minimal processing of received packets, then puts the packets that pass certain tests onto a work queue (by "packet" I mean information such as what was received, and the origin socket address). A second pthread handles the work queue - it pops a packet off the queue, processes it, then uses sentto() to reply to the original requestor.

Unfortunately, with this paradigm, I am going to be calling recvfrom() and sendto() on the same socket concurrently from 2 different threads. Is this safe? I want to use the same socket for both recvfrom() and sendto() because I want the response packet coming from the server have the same port numbers as the request packet (that is, if my service is running on port 10020, I want the server's responses to come from that port too).

Any thoughts on this? Thread safety?
 
Back
Top