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时的几个注意事项(Mirosoft)相关推荐

  1. 编写Javascript时的注意事项

    编写Javascript时的注意事项: 1.预留退路 网站的访问者完全有可能使用的是不支持JavaScript的浏览器,有一些可能是浏览器不支持,或者是浏览器支持,但用户为了避免讨厌的弹出广告而仅用了 ...

  2. RPGMAKER游戏引擎基于JavaScript的插件制作(二)——如何理解插件,以及编写代码时的注意事项

    之前写的时候不想写这部分就先跳过了,于是拖到现在- 这篇主要作用是做一些细节上的整理,根据我的经验不断完善,也欢迎在评论中补充. 重要的知识放在重写方法部分. 2. 1如何理解插件 我认为读我这篇博客 ...

  3. mysql编写中文时变成问号解决方法

    mysql编写中文时变成问号解决方法 参考文章: (1)mysql编写中文时变成问号解决方法 (2)https://www.cnblogs.com/yangzhixue/p/11449525.html ...

  4. idea修改java和jsp不起作用_使用IDEA编写jsp时EL表达式不起作用的问题及解决方法...

    在使用IDEA开发maven+springMVC项目时遇到不加载EL表达式的问题,怎么处理呢?下面小编给大家带来了实现代码,一起看看吧 加载如下JSP代码: Title ${lists.usernam ...

  5. SQL Server 2008编写脚本时智能提示功能丢失的处理

    SQL Server 2008编写脚本时智能提示功能丢失的处理 1.查看工具选项中[启用IntelliSense]是否打开: 选择[工具]-->[选项]打开选项页面,依次展开[文本编辑器]--& ...

  6. 在编写异步方法时,使用 ConfigureAwait(false) 避免使用者死锁

    我在 使用 Task.Wait()?立刻死锁(deadlock) 一文中站在类库使用者的角度看 async/await 代码的死锁问题:而本文将站在类库设计者的角度来看死锁问题. 阅读本文,我们将知道 ...

  7. C语言试题六十二之请编写函数fun除了字符串前导和尾部的*号之外,将串中其他*号全部删除。形参h已指向字符串第一个字母,形参p已指向字符串中最后一个字母。在编写程序时,不得使用c语言提供的字符串函数。

    1. 题目 请编写函数fun除了字符串前导和尾部的*号之外,将串中其他*号全部删除.形参h已指向字符串第一个字母,形参p已指向字符串中最后一个字母.在编写程序时,不得使用c语言提供的字符串函数. 比如 ...

  8. C语言试题五十七之假定输入的字符串中只包含字母和*号。请编写函数function,它的功能是:删除字符串中所有*号。在编写函数时,不得使用c语言提供的字符串函数。

    1. 题目 假定输入的字符串中只包含字母和*号.请编写函数function,它的功能是:删除字符串中所有*号.在编写函数时,不得使用c语言提供的字符串函数. 2 .温馨提示 C语言试题汇总里可用于计算 ...

  9. 汇编语言-008(条件跳转应用 、loopnz 条件循环指令使用 、loopz 条件循环指令使用 、 if语句编写汇编时 、while语句编写汇编时 、循环内的if语句嵌套)

    1: 条件跳转应用,三个数中最小数 .386 .model flat,stdcall.stack 4096 ExitProcess PROTO,dwExitCode:DWORD.data V1 WOR ...

最新文章

  1. #tomcat#生成的jsp转换问题
  2. springboot yml怎么建常量_Springboot中加载自定义的yml配置文件
  3. 2019春季学期第四周作业
  4. 关于pytorch里面的图像变换
  5. php单选框点击取消,取消选中单选框radio的三种方式
  6. windows版一键绕id工具_【实用工具】一键关闭/开启Windows防火墙、禁止更新系统...
  7. 技术:叫我怎么爱上你
  8. Deepin-文件目录介绍
  9. 三星手机怎么把html,三星手机怎么连接电脑 只要四步轻松搞定【图文】
  10. 在idea或eclipse软件下配置Tomcat
  11. mongodb mapredReduce 多个条件分组(group by)
  12. 巴斯克维尔字体标本案例研究
  13. 如何在团队内做技术分享
  14. OK6410移植UBOOT
  15. 精打视频教程(10)菜鸟电子面单打印与发货
  16. 对于谷歌搜索的一些基本语法整理
  17. 切!原来进入500强就那么简单啊——前IBM,HP,Dell员工揭开外企的招聘内幕
  18. Fedora13 添加 网易镜像源
  19. 牛牛找工作--网易2019实习生招聘编程题
  20. HTML5 中 40 个最重要的技术点

热门文章

  1. 网页页面禁止用户复制
  2. 数据库学习--DQL(数据库查询语言)
  3. python正则_python中正则匹配
  4. DirectX 学习笔记
  5. Monostate模式
  6. Shell 基础知识--细说linux配套视频
  7. 关于sql server 的那些事
  8. JGroups - 02架构概述
  9. 冒泡排序 c语言,冒泡排序(C语言)
  10. Node+fs+定时器(node-schedule)+MySql