I am currently developing a networking software that uses a datagram socket (UDP) to send data to clients. Whenever I'd like to send data to a client I am currently invoking sendto() and passing the respective parameters. Yet I am wondering whether or not making a blocking call to sendto() from multiple threads at the same time is good idea or whether data might get interleaved or corrupted in some other way.
I have already found this answer: is winsock2 thread safe? but I am not sure if this holds true for sendto() as it does for send()
System calls are not atomic, you can't assume they are thread safe. Thread safety depends on the system implementation. But thread safety just means you won't encounter crashes or memory corruption, it doesn't tell you anything about the behaviour. For example, you may have data interleaved in what you sent, with no respect to your threads calling order.
If you're working on Windows, Winsock2 seems to be thread safe on recent versions of the os. But once again, it doesn't mean it'll behave as you expect.
Rather than using several threads to send to or receive from a socket, you should consider using IO ports, which are meant for multithreading and asynchronous processing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With