`
aigo
  • 浏览: 2635689 次
  • 性别: Icon_minigender_1
  • 来自: 宜昌
社区版块
存档分类
最新评论

编写IOCP时的几个注意事项(Mirosoft官方)

阅读更多

 

官方:https://support.microsoft.com/en-us/kb/192800

原文:http://www.xuebuyuan.com/833696.html

 

TIP 1: Use Winsock2 IOCP-capable functions, such as WSASend and WSARecv, over Win32 file I/O functions, such as WriteFile and ReadFile.

 

提示1:尽量使用WSASend和WSARecv进行数据收发

 

Socket handles from Microsoft-based protocol providers are IFS handles so you can use Win32 file I/O calls with the handle. However, the interactions between the provider and file system involve many kernel/user mode transition, thread context switches, and parameter marshals that result in a significant performance penalty. You should use only Winsock2 IOCP- capable functions with IOCP.

 

The additional parameter marshals and mode transitions in ReadFile and WriteFile only occur if the provider does not have XP1_IFS_HANDLES bit set in dwServiceFlags1 of its WSAPROTOCOL_INFO structure.

 

NOTE: These providers have an unavoidable additional mode transition, even in the case of WSASend and WSARecv, although ReadFile and WriteFile will have more of them.

 

TIP 2: Choose the number of the concurrent worker threads allowed and the total number of the worker threads to spawn.

 

提示2:确定合适的工作者线程数目

 

The number of worker threads and the number of concurrent threads that the IOCP uses are not the same thing. You can decide to have a maximum of 2 concurrent threads used by the IOCP and a pool of 10 worker threads. You have a pool of worker threads greater than or equal to the number of concurrent threads used by the IOCP so that a worker thread handling a dequeued completion packet can call one of the Win32 "wait" functions without delaying the handling of other queued I/O packets.

 

If there are completion packets waiting to be dequeued, the system will wake up another worker thread. Eventually, the first thread satisfies it's Wait and it can be run again. When this happens, the number of the threads that can be run is higher than the concurrency allowed on the IOCP (for example, NumberOfConcurrentThreads). However, when next worker thread calls GetQueueCompletionStatus and enters wait status, the system does not wake it up. In other words, the system tries to keep your requested number of concurrent worker threads.

 

Typically, you only need one concurrent worker thread per CPU for IOCP. To do this, enter 0 for NumberOfConcurrentThreads in the CreateIoCompletionPort call when you first create the IOCP.

 

TIP 3: Associate a posted I/O operation with a dequeued completion packet.

 

提示3:根据出队的完成包进行IO操作。

 

GetQueuedCompletionStatus returns a completion key and an overlapped structure for the I/O when dequeuing a completion packet. You should use these two structures to return per handle and per I/O operation information, respectively. You can use your socket handle as the completion key when you register the socket with the IOCP to provide per handle information. To provide per I/O operation "extend" the overlapped structure to contain your application-specific I/O-state information. Also, make sure you provide a unique overlapped structure for each overlapped I/O. When an I/O completes, the same pointer to the overlapped I/O structure is returned.

 

TIP 4: I/O completion packet queuing behavior.

 

提示4:完成包的异常处理。

 

The order in which I/O completion packets are queued in the IOCP is not necessarily the same order the Winsock2 I/O calls were made. Additionally, if a Winsock2 I/O call returns SUCCESS or IO_PENDING, it is guaranteed that a completion packet will be queued to the IOCP when the I/O completes, regardless of whether the socket handle is closed. After you close a socket handle, future calls to WSASend, WSASendTo, WSARecv, or WSARecvFrom will fail with a return code other than SUCCESS or IO_PENDING, which will not generate a completion packet. The status of the completion packet retrieved by GetQueuedCompletionStatus for I/O previously posted could indicate a failure in this case.

 

If you delete the IOCP itself, no more I/O can be posted to the IOCP because the IOCP handle itself is invalid. However, the system's underlying IOCP kernel structures do not go away until all successfully posted I/Os are completed.

 

TIP 5: IOCP cleanup.

 

提示5:完成端口的释放。

 

The most important thing to remember when performing ICOP cleanup is the same when using overlapped I/O: do not free an overlapped structure if the I/O for it has not yet completed. The HasOverlappedIoCompleted macro allows you to detect if an I/O has completed from its overlapped structure.

 

There are typically two scenarios for shutting down a server. In the first scenario, you do not care about the completion status of outstanding I/Os and you just want to shut down as fast as you can. In the second scenario, you want to shut down the server, but you do need to know the completion status of each outstanding I/O.

 

In the first scenario, you can call PostQueueCompletionStatus (N times, where N is the number of worker threads) to post a special completion packet that informs the worker thread to exit immediately, close all socket handles and their associated overlapped structures, and then close the completion port. Again, make sure you use HasOverlappedIoCompleted to check the completion status of an overlapped structure before you free it. If a socket is closed, all outstanding I/O on the socket eventually complete quickly.

 

In the second scenario, you can delay exiting worker threads so that all completion packets can be properly dequeued. You can start by closing all socket handles and the IOCP. However, you need to maintain a count of the number of outstanding I/Os so that your worker thread can know when it is safe to exit the thread. The performance penalty of having a global I/O counter protected with a critical section for an IOCP server is not as bad as might be expected because the active worker thread does not switch out if there are more completion packets waiting in the queue.

 

 

分享到:
评论

相关推荐

    学习IOCP时写的实例

    标题中的“学习IOCP时写的实例”表明这是一个关于IO完成端口(IOCP,Input/Output Completion Port)的编程示例。IOCP是Windows操作系统中一种高效处理大量并发I/O操作的技术,尤其适用于网络服务器的开发,能显著...

    iocp 一个实现不错的类

    标题中的"Iocp 一个实现不错的类"指的是一个实现了IOCP概念的类,可能是用C++或其他支持Windows API的语言编写的。 IOCP的核心思想是将I/O操作与处理结果的回调分离,当设备完成I/O操作后,操作系统会将结果放入一...

    IOCP高性能服务器

    在设计IOCP服务器时,通常包括以下几个关键步骤: 1. **创建IOCP**:首先,我们需要调用`CreateIoCompletionPort`函数来创建一个IOCP。这个端口将作为所有I/O操作完成的通知中心。 2. **绑定Socket到IOCP**:接着...

    一个对IOCP进行封装的DLL

    这个“IOCP的dll”是由一位外国开发者编写的,它的核心价值在于简化了IOCP的使用。开发者只需加载该DLL,并调用其中的接口函数,即可实现服务器的创建、连接管理、数据传输等功能。这种方式降低了使用IOCP的门槛,...

    简单iocp模型例子

    一个简单的IOCP服务器通常包括以下几个主要部分: 1. **初始化**:创建IOCP,设置监听套接字,并将其与IOCP关联。 2. **接受连接**:当有新的客户端连接请求时,服务器调用`AcceptEx`函数异步接受连接。这个函数会...

    IOCP文件客户端 IOCP文件客户端

    IOCP(Input/Output Completion Port,输入/输出完成端口)是Windows操作系统提供的一种高度优化的I/O模型,尤其适用于高并发的网络编程场景。...理解并掌握IOCP的工作机制对于编写高性能的网络应用至关重要。

    vc以IOCP完成端口方式实现网络数据传输 iocp服务器+客户端.zip

    使用IOCP时,需要注意错误处理,比如确保所有I/O操作都被正确地提交到完成端口,以及在接收到错误通知时能及时处理。同时,调试IOCP程序需要对异步编程有深入理解,因为错误可能在不同的线程和时间点出现。 总结来...

    带可执行文件和源代码的聊天室IOCP纯C语言实现

    标题中的“带可执行文件和源代码的聊天室IOCP纯C语言实现”指的是一个使用Intel I/O Completion Ports (IOCP)技术,并且完全用C语言编写的聊天室应用程序。IOCP是Windows操作系统中用于提高网络I/O性能的一种高级...

    VC的IOCP开发,iocp类,demo

    当使用IOCP时,我们可以将Socket的读写操作绑定到IOCP上,当读写操作完成时,系统会自动将完成信息放入IOCP,然后由我们的代码负责处理这些完成信息,这样可以避免了传统同步阻塞I/O的线程阻塞问题。 三、VC++实现...

    windows IOCP文档

    IOCP的挑战与注意事项 - **线程同步**:尽管IOCP提供了高效的并发处理,但线程间的同步问题仍需谨慎处理,防止数据竞争。 - **错误处理**:正确处理I/O失败情况,包括网络中断、内存不足等问题。 - **资源管理**:...

    IOCP原理windows下实现

    IOCP的实现过程主要包括以下几个步骤: 1. **创建IOCP**:首先,需要调用`CreateIoCompletionPort`函数创建一个IOCP对象。这个对象用于存储和管理所有与之关联的I/O操作完成信息。 2. **关联设备或套接字**:接着...

    iocp开发一个最简单的例子

    本篇文章将通过一个最简单的IOCP示例,深入探讨其工作原理和使用方法。 首先,我们来看看IOCP的基本概念。IOCP是Windows系统提供的一种I/O模型,它允许应用程序通过单一的线程池来处理所有的I/O完成事件,极大地...

    IOCP服务器模块.rar

    四、性能优化与注意事项 1. 线程池管理:根据系统资源和负载动态调整工作线程数量,避免过多线程导致的开销。 2. 错误处理:对可能出现的错误进行妥善处理,如网络中断、内存不足等。 3. 适当缓冲:使用缓冲区减少...

    完整的IOCP实例

    在这个实例中,VS2010作为开发环境,提供了强大的调试和代码管理功能,便于开发者编写、测试和维护IOCP代码。项目可能包含了服务器的核心逻辑,如连接管理、数据包解析、业务处理等模块,以及用于测试客户端的模拟...

    vc语言编程之iocp系统

    三、VC++中使用IOCp的注意事项 3.1 线程池管理 为了高效处理I/O事件,通常需要创建一个线程池。每个线程都可以调用GetQueuedCompletionStatus来接收并处理I/O完成通知。线程池的数量应根据系统的实际负载进行调整...

    IOCP完成端口原理

    IOCP 完成端口原理 一、IOCP 简介 IOCP(I/O Completion Port)是一种异步I/O模型,用于提高服务器的...通过理解IOCP的基本架构、实现高并发服务器的方法和IOCP开发的几个概念,可以更好地开发高性能的服务器应用。

    windows iocp网络通讯库封装

    这个库可能是用C++编写的,它提供了面向对象的接口,使得开发者可以更方便地使用IOCP进行网络通信。可能包含如`ITcpClient`和`ITcpServer`这样的类,分别代表TCP客户端和服务器,提供创建、连接、监听、发送和接收...

    IOCP客户端模拟程序

    在设计IOCP客户端时,我们需要关注以下几个关键步骤: 1. 创建IOCP:通过`CreateIoCompletionPort`函数创建IOCP,设置适当的参数,如最大并发请求数等。 2. 创建套接字:使用`socket`函数创建用于网络通信的套接字...

    mfc 网络编程iocp实例

    7. **优化和注意事项** 在实际应用中,为了提高效率,我们需要合理配置线程池大小,避免过多线程导致的上下文切换开销。同时,要处理好异常情况,如网络中断、内存泄漏等问题,确保程序的稳定性和可靠性。 总的来...

Global site tag (gtag.js) - Google Analytics