[英]Is it thread safe to call socket send to different sockets from different threads?
Hi I am new to C++/C and I have a question: Is it thread safe to call socket send to different sockets from different threads?嗨,我是 C++/C 的新手,我有一个问题:从不同的线程调用套接字发送到不同的 sockets 是线程安全的吗? I can find answer on stackoverflow that message may be jumbled upon receive when calling send() to SAME socket from different threads.
我可以在 stackoverflow 上找到答案,当从不同线程调用 send() 到 SAME 套接字时,消息可能会在接收时混乱。 What about calling send() to DIFFERENT sockets from different threads?
从不同的线程调用 send() 到不同的 sockets 怎么样? Is it thread safe?
它是线程安全的吗? Thank you.
谢谢你。
#include <sys/types.h>
#include <sys/socket.h>
send(1, buf1, buf1.size(), 0); //call from thread1
send(2, buf2, buf2.size(), 0); //call from thread2
It is perfectly safe for separate threads to call send()
to different sockets at the same time.单独的线程同时调用
send()
到不同的 sockets 是完全安全的。
Like you said, the only time you need to worry is when separate threads are trying to send()
to the same socket at the same time, that needs to be coordinated accordingly.就像你说的,你唯一需要担心的是当不同的线程试图同时将
send()
到同一个套接字时,需要相应地进行协调。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.