是winsock2线程安全吗? [英] is winsock2 thread safe?

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

问题描述

我正在写一个小型的3台服务器和1个客户端程序。 2个服务器发送tcp消息,最后一个服务器使用winsock2发送upd数据报。

I am writing a small 3 servers and 1 client program. the 2 servers send tcp messages and the last one sends upd datagrams using winsock2.

我想知道如果我可以通过使用线程(OpenMP或boost :: threads)使类似recvfrom(),使2个线程从同一个端口侦听同一个端口与此同时。

I am wondering if i can make simulanious recvfrom() by using threads (OpenMP or boost::threads) so that 2 threads listen from the same socket on the same port at the same time.

我在Windows7上使用VC ++ 2010。

I am using VC++ 2010 on windows7.

感谢您的帮助。

推荐答案

是的,套接字是线程安全的,但是你必须小心。一个常见的模式(使用阻塞IO时)是让一个线程在套接字上接收数据,另一个线程在同一个套接字上发送数据。有多个线程从套接字接收数据通常对UDP套接字很好,但在大多数时候对TCP套接字没有意义。在文档中有关于 WSARecv

Yes, sockets are thread-safe, however you have to be careful. One common pattern (when using blocking IO) is to have one thread receiving data on a socket and another thread sending data on the same socket. Having multiple threads receiving data from a socket is usually fine for UDP socket, but doesn't make much sense for TCP sockets most of the time. There is a warning in the documentation for WSARecv:


WSARecv不应该从
不同线程同时在同一个套接字上调用,因为它可能导致不可预知的缓冲
顺序。

WSARecv should not be called on the same socket simultaneously from different threads, because it can result in an unpredictable buffer order.

但是,如果你使用UDP,协议是无状态的,这通常不会引起任何关注。

But this usually isn't of any concern if you are using UDP and the protocol is stateless.

另请注意, WSAEINPROGRESS 错误代码主要适用于Winsock 1.1:

Also note that the WSAEINPROGRESS error code mainly applies to Winsock 1.1:


WSAEINPROGRESS:正在进行阻止的Windows Sockets 1.1调用,或者服务提供程序仍在处理回调函数。

WSAEINPROGRESS: A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function.

并且 WSAEINPROGRESS 的描述进一步说明:

And the description of WSAEINPROGRESS further states:


正在进行操作。

Operation now in progress.

正在执行阻止操作。 Windows Sockets只允许单个阻塞操作(任务或线程)未完成,并且如果进行任何其他函数调用(不管它是否引用该任何套接字或任何其他套接字),该函数将失败,并显示WSAEINPROGRESS错误。

A blocking operation is currently executing. Windows Sockets only allows a single blocking operation—per- task or thread—to be outstanding, and if any other function call is made (whether or not it references that or any other socket) the function fails with the WSAEINPROGRESS error.

请注意,这涉及每个任务或线程的单个阻止操作。

Note that this talks about a single blocking operation per-task or thread.

此外,WSARecv的文档中还有一个警告:

Furthermore there is an additional warning in the documentation for WSARecv:


在APC中发出另一个阻塞Winsock调用,中断正在进行的阻塞Winsock调用

Issuing another blocking Winsock call inside an APC that interrupted an ongoing blocking Winsock call on the same thread will lead to undefined behavior, and must never be attempted by Winsock clients.

但是除了这些警告,你应该是很好的。

But apart from those warnings you should be fine.

更新:添加一些外部引用:
alt.winsock.programming:is socket thread-safe?
Winsock程序员常见问题:Winsock是否线程安全?

Update: to add some external references: alt.winsock.programming: Is socket thread-safe? and Winsock Programmer’s FAQ: Is Winsock thread-safe?

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

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