is DatagramSocket.send thread safe?

梦想的初衷 提交于 2019-12-06 18:11:02

问题


I'm planning to use an instance of DatagramSocket and call its send method from different threads... to send UDP packets to different clients. Is the method thread safe i.e. calling this method from different threads will not create any trouble/inconsistency/race condition?

Thanks!


回答1:


UDP guarantees that datagrams arrive intact (if at all). In other words there can be no interleaving even if there is multithreading at the sender. That's all you need. You don't actually need thread safety. However the C send() system call is thread safe, like all system calls, because they are atomic.




回答2:


Yes. This is only a thin layer on the native OS, which is threadsafe.

See here http://www.velocityreviews.com/forums/t150685-is-datagramsocket-thread-safe.html



来源:https://stackoverflow.com/questions/16498223/is-datagramsocket-send-thread-safe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!