是DatagramSocket.send线程安全吗? [英] is DatagramSocket.send thread safe?

查看:425
本文介绍了是DatagramSocket.send线程安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我计划使用 DatagramSocket 的实例,并调用从不同的线程发送方法...发送UDP数据包到不同的客户端。是方法线程安全的,即从不同的线程调用此方法不会创建任何麻烦/不一致/竞争条件?

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?

谢谢!

推荐答案

UDP保证数据报完好无损地到达(如果有的话)。换句话说,即使在发送器处存在多线程,也可以不存在交织。这就是你需要的。你实际上不需要线程安全。然而,C send()系统调用是线程安全的,像所有的系统调用一样,因为它们是原子的。

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.

这篇关于是DatagramSocket.send线程安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆