简体   繁体   English

从不同的线程调用套接字发送到不同的 sockets 是否线程安全?

[英]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.

相关问题 如何使用C套接字接收和发送不同的线程 - How to receive and send in different threads with C sockets 从两个不同的线程在两个不同的索引处写入浮点数组是否安全? - Is writing to an array of floats from 2 different threads at two different indexes safe? 如何让一个线程等待来自不同线程的多个条件信号? - How to make a thread wait for multiple conditional signals from different threads? 如何在来自不同线程的线程上退出对 recv() 的阻塞调用? - How to exit a blocking call of recv() on a thread from a different thread? 使用带有线程的c套接字读取和写入套接字的问题 - Issue using c sockets with threads to read from and write to the socket 访问 C 数组线程中的不同元素是否安全? - Is access to different elements in a C array thread safe? 不同线程的相同线程 ID 的问题 - Problem with same thread ID for different threads 一个线程可以由不同的线程创建和加入吗? - Can a thread be created and joined by different threads? 服务器可以从其他进程和套接字发送响应吗? - Can server send response from a different process and socket? 使用phtreads从C中的多个线程在不同位置读取和写入数组是否安全? - Is it safe to read and write to an array at different positions from multiple threads in C with phtreads?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM