.Net中的I/O thread vs. worker thread

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类型。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值