简体   繁体   English

如何让Win32线程在工作队列和套接字上等待?

[英]How to get a Win32 Thread to wait on a work queue and a socket?

I need a client networking thread to be able to respond both to new messages to be transmitted, and the receipt of new data on the network. 我需要一个客户端网络线程,以便能够响应要传输的新消息以及在网络上接收新数据。 I wish to avoid this thread performing a polling loop, but rather to process only as needed. 我希望避免这个线程执行轮询循环,而只是根据需要处理。

The scenario is as follows: 方案如下:

A client application needs to communicate to a server via a protocol that is largely, but not entirely, synchronous. 客户端应用程序需要通过主要但非完全同步的协议与服务器通信。 Typically, the client sends a message to the server and blocks until a response is received. 通常,客户端向服务器发送消息并阻塞,直到收到响应。

The server may process client requests asynchronously, in which case the response to client is not a result, but a notification that processing has begun. 服务器可以异步处理客户端请求,在这种情况下,对客户端的响应不是结果,而是处理已经开始的通知。 A result message is sent to to the client at some point in the future, when the server has finish processing the client request. 当服务器完成客户端请求的处理时,结果消息将在以后的某个时间点发送到客户端。

The asynchronous result notifications can arrive at the client at any time. 异步结果通知可以随时到达客户端。 These notifications need processed when they are received ie it is not possible to process a backlog only when the client transmits again. 这些通知在收到时需要处理,即只有在客户端再次发送时才能处理积压。

The clients networking thread receives and processes notifications from the server, and to transmit outgoing messages from the client. 客户端网络线程接收并处理来自服务器的通知,并从客户端传输传出消息。

To achieve this, I need to to make a thread wake to perform processing either when network data is received OR when a message to transmit is enqueued into an input queue. 为了实现这一点,我需要做一个线程唤醒,以便在接收到网络数据时或者当要传输的消息入队到输入队列时执行处理。

How can a thread wake to perform processing of an enqueued work item OR data from a socket? 线程如何唤醒以执行从入口处理入队工作项或数据的处理?

I am interested primarily in using the plain Win32 APIs. 我主要感兴趣的是使用普通的Win32 API。

A minimal example or relevant tutorial would be very welcome! 非常欢迎一个最小的例子或相关的教程!

An alternative to I/O Completion Ports for sockets is using WSAEventSelect to associate an event with the socket. 套接字的I / O完成端口的替代方法是使用WSAEventSelect将事件与套接字关联。 Then as others have said, you just need to use another event (or some sort of waitable handle) to signal when an item has been added to your input queue, and use WaitForMultipleObjects to wait for either kind of event. 然后正如其他人所说,你只需要使用另一个事件(或某种可等待的句柄)来表示何时将一个项添加到输入队列,并使用WaitForMultipleObjects等待任何一种事件。

You can set up an I/O Completion Port for the handles and have your thread wait on the completion port: 您可以为句柄设置I / O完成端口,让您的线程在完成端口上等待:

Actually, you can have multiple threads wait on the port (one thread per processor usually works well). 实际上,您可以让多个线程在端口上等待(每个处理器一个线程通常运行良好)。

Following on from Michael's suggestion, I have some free code that provides a framework for IO Completion Port style socket stuff; 根据Michael的建议,我有一些免费的代码,为IO Completion Port样式套接字提供了一个框架; and it includes an IOCP based work queue too. 它还包括一个基于IOCP的工作队列。 You should be able to grab some stuff from it to solve your problem from here . 您应该能够从中抓住一些东西,从解决您的问题在这里

Well, if both objects have standard Windows handles, you can have your client call WaitForMultipleObjects to wait on them. 好吧,如果两个对象都有标准的Windows句柄,您可以让客户端调用WaitForMultipleObjects来等待它们。

You might want to investiate splitting the servicing of the network port off onto its own thread. 您可能希望将网络端口的服务拆分为自己的线程。 That might simplify things greatly. 这可能会大大简化事情。 However, it won't help if you just end up having to synchonize something else between that new thread and your main one. 但是,如果您最终必须在新线程和主线程之间同步其他内容,那将无济于事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM