从多个线程中的同一UDP套接字发送 [英] Sending from the same UDP socket in multiple threads

查看:322
本文介绍了从多个线程中的同一UDP套接字发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个线程需要将UDP数据包发送到不同的IP地址(仅用于发送,不需要接收任何内容).我可以在所有线程中重用同一个UDP套接字吗?

I have multiple threads which need to send UDP packets to different IP addresses (only to send, nothing needs to be received). Can I reuse the same UDP socket in all the threads?

推荐答案

是的,我想你可以.

由于数据包是单独发送的,尽管它们的接收顺序不确定,但已经使用UDP.

As the packets are sent out individually, although the order they are received will be nondeterministic, it is already with UDP.

因此在同一套接字中发送多个线程是可以的.

So sending in multiple threads in the same socket is fine.

尽管,如果您正在使用套接字执行其他操作,例如bind(),close(),那么您可能会遇到竞争条件,因此您可能需要小心.

Although, if you're doing other stuff with the socket, such as bind(), close(), then you could end up with race conditions, so you might want to be careful.

这篇关于从多个线程中的同一UDP套接字发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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