Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is DatagramSocket.send thread safe?

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!

like image 575
Shafiul Avatar asked May 11 '13 14:05

Shafiul


2 Answers

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

like image 92
BraveNewCurrency Avatar answered Oct 15 '22 13:10

BraveNewCurrency


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.

like image 27
user207421 Avatar answered Oct 15 '22 11:10

user207421