loop_in_codes

低调做技术__欢迎移步我的独立博客 codemaro.com 微博 kevinlynx

IOCP与线程

author : Kevin Lynx

 

什么是完成包?

完成包,即IO Completion Packet,是指异步IO操作完毕后OS提交给应用层的通知包。IOCP维护了一个IO操作结果队列,里面
保存着各种完成包。应用层调用GQCS(也就是GetQueueCompletionStatus)函数获取这些完成包。

最大并发线程数

在一个典型的IOCP程序里,会有一些线程调用GQCS去获取IO操作结果。最大并发线程数指定在同一时刻处理完成包的线程数目。
该参数在调用CreateIoCompletionPort时由NumberOfConcurrentThreads指定。

工作者线程

工作者线程一般指的就是调用GQCS函数的线程。要注意的是,工作者线程数和最大并发线程数并不是同一回事(见下文)。工作者
线程由应用层显示创建(_beginthreadex 之类)。工作者线程通常是一个循环,会不断地GQCS到完成包,然后处理完成包。

调度过程

工作者线程以是否阻塞分为两种状态:运行状态和等待状态。当线程做一些阻塞操作时(线程同步,甚至GQCS空的完成队列),线程
处于等待状态;否则,线程处于运行状态。

另一方面,OS会始终保持某一时刻处于运行状态的线程数小于最大并发线程数。每一个调用GQCS函数的线程OS实际上都会进行记录,
当完成队列里有完成包时,OS会首先检查当前处于运行状态的工作线程数是否小于最大并发线程数,如果小于,OS会按照LIFO的顺
序让某个工作者线程从GQCS返回(此工作者线程转换为运行状态)。如何决定这个LIFO?这是简单地通过调用GQCS函数的顺序决定的。

从这里可以看出,这里涉及到线程唤醒和睡眠的操作。如果两个线程被放置于同一个CPU上,就会有线程切换的开销。因此,为了消
除这个开销,最大并发线程数被建议为设置成CPU数量。

从以上调度过程还可以看出,如果某个处于运行状态的工作者线程在处理完成包时阻塞了(例如线程同步、其他IO操作),那么就有
CPU资源处于空闲状态。因此,我们也看到很多文档里建议,工作者线程数为(CPU数*2+2)。

在一个等待线程转换到运行状态时,有可能会出现短暂的时间运行线程数超过最大并发线程数,这个时候OS会迅速地让这个新转换
的线程阻塞,从而减少这个数量。(关于这个观点,MSDN上只说:by not allowing any new active threads,却没说明not allowing
what)

调度原理

这个知道了其实没什么意义,都是内核做的事,大致上都是操作线程control block,直接摘录<Inside IO Completion Ports>:

The list of threads hangs off the queue object. A thread's control block data structure has a pointer in it that
references the queue object of a queue that it is associated with; if the pointer is NULL then the thread is not
associated with a queue.

So how does NT keep track of threads that become inactive because they block on something other than the completion
port" The answer lies in the queue pointer in a thread's control block. The scheduler routines that are executed
in response to a thread blocking (KeWaitForSingleObject, KeDelayExecutionThread, etc.) check the thread's queue
pointer and if its not NULL they will call KiActivateWaiterQueue, a queue-related function. KiActivateWaiterQueue
decrements the count of active threads associated with the queue, and if the result is less than the maximum and
there is at least one completion packet in the queue then the thread at the front of the queue's thread list is
woken and given the oldest packet. Conversely, whenever a thread that is associated with a queue wakes up after
blocking the scheduler executes the function KiUnwaitThread, which increments the queue's active count.

参考资料

<Inside I/O Completion Ports>:
http://technet.microsoft.com/en-us/sysinternals/bb963891.aspx
<I/O Completion Ports>:
http://msdn.microsoft.com/en-us/library/aa365198(VS.85).aspx
<INFO: Design Issues When Using IOCP in a Winsock Server>:
http://support.microsoft.com/kb/192800/en-us/

posted on 2008-06-23 17:32 Kevin Lynx 阅读(4793) 评论(3)  编辑 收藏 引用 所属分类: network

评论

# re: IOCP与线程 2008-06-23 23:45 饭中淹

@Kevin Lynx
写iocp应用的时候,我觉得最好能够实现工作线程就地组包,然后在组包之后,在工作线程内就地处理,这样是对iocp和cpu资源的最佳化利用。

还有,多谢你的帮助,我才纠正了对iocp的错误理解。

  回复  更多评论   

# re: IOCP与线程 2010-06-04 11:02 我是猪

@饭中淹
错了 这样会浪费效能  回复  更多评论   

# re: IOCP与线程 2010-06-29 09:55 zhengxf

你好,我也认为写iocp应用的时候, 应该工作线程就地组包,然后在组包之后,在工作线程内就地处理, 不知道"饭中淹" 老兄有何高见,能分享一下吗.
  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理