tags:

views:

75

answers:

2

I have multiple threads which need to send UDP packets to different IP addresses (only to send, nothing needs to be received). Can I reuse the same UDP socket in all the threads?

+1  A: 

System calls are supposed to be atomic, so formally it seems fine for UDP. Then kernels have bugs too and you are inviting all sorts of nasty surprises. Why can't you use socket per thread? It's not like with TCP where you need a connection. As an added bonus you'd get a separate send buffer for each descriptor.

Nikolai N Fetissov
+2  A: 

Yes, I think you can.

As the packets are sent out individually, although the order they are received will be nondeterministic, it is already with UDP.

So sending in multiple threads in the same socket is fine.

Although, if you're doing other stuff with the socket, such as bind(), close(), then you could end up with race conditions, so you might want to be careful.

MarkR