Stackoverflow.com上的一个post(http://stackoverflow.com/questions/2099947/simple-description-of-worker-and-i-o-threads-in-net)关于它们的解释比较准确,原文摘录如下:
“The term 'I/O thread' in.net/CLR refers to the threads the ThreadPool reserves in order to dispatchNativeOverlapped callbacks from"overlapped" win32 calls (also known as "completion portI/O"). The CLR maintains its own I/O completion port, and can bind anyhandle to it (via the ThreadPool.BindHandle API).
“There is really no technical difference between 'worker thread' and 'I/O thread' -- they are both justnormal threads. But the CLR ThreadPool keeps separate pools of each simply toavoid a situation where high demand on worker threads exhausts all the threadsavailable to dispatch native I/O callbacks, potentially leading to deadlock.(Imagine an application using all 250 worker threads, where each one is waitingfor some I/O to complete).”
需要注意的是:Vista之前的Win32中有2个类似的概念:I/O worker thread和non-I/O worker thread,这两个概念是针对Win32thread pool提出来的,它们的定义和区别可以引用MSDN的话:
“An I/O worker thread is athread that waits in an alertable wait state. Work items are queued to I/Oworker threads as Asynchronous Procedure Calls (APC).”,“Anon-I/Oworker thread waits on I/O completion ports.”。
几点注释:
1. 据MSDN,.Net提供的NativeOverlapped就是Win32提供的OVERLAPPED结构,同时尾部附带一些bytes(不在此追究)。在.Net DllImport时,CLR在native DLL中解析(寻找)函数时,只关心函数的名称,不关心函数的参数情况,即解析过程中不存在C++中的函数overload的概念,亦即函数的signature不考虑参数(这是C的做法)。
2. .Net库已经把Overlapped IO进行了包装,一般情况下,不需要直接使用这些原始元素来完成我们手头的任务。但是,这个机制在.Net下是存在的并且是可用的。
3. .Net thread pool中的I/O worker thread似乎刚好就是Win32threadpool中的non-I/O worker thread。Microsoft®不应该制造这样的confusion,所幸的是,Windows® Vista之后的thread pool已经抛弃了这两个概念,转而使用了统一的thread类型。