Sending from the same UDP socket to multiple threads

I have several streams that should send UDP packets to different IP addresses (only for sending, nothing needs to be received). Is it possible to reuse the same UDP socket in all threads?

+3
source share
2 answers

Yes, I think you can.

Since packets are sent individually, although the order they receive is non-deterministic, it already has UDP.

Thus, sending multiple threads on the same socket is wonderful.

Although, if you do other things using a socket, for example bind (), close (), then you may encounter race conditions, so you can be careful.

+3
source

System calls should be atomic, so formally this is normal for UDP. Then the cores also have errors, and you invite all sorts of unpleasant surprises. Why can't you use socket on stream? This is not like TCP where you need a connection. As an added bonus, you will receive a separate send buffer for each descriptor.

+2
source

All Articles