简体   繁体   English

我什么时候调用BeginXXX()时CLR创建IO线程

[英]When does the CLR create the IO thread when I call BeginXXX()

Suppose I call HttpWebRequest.BeginGetRequestStream() method. 假设我调用了HttpWebRequest.BeginGetRequestStream()方法。

I know that this method is asynchronous and when it's finished, an callback method should be called. 我知道这个方法是异步的,当它完成时,应该调用一个回调方法。

What I'm very sure is that when call the callback method, an I/O thread is created by CLR and this I/O thread do the callback. 我非常确定的是,当调用回调方法时,CLR会创建一个I / O线程,这个I / O线程会进行回调。

What I'm not sure is when I call HttpWebRequest.BeginGetRequestStream(), is there any I/O thread created by CLR? 我不确定的是当我调用HttpWebRequest.BeginGetRequestStream()时,是否有CLR创建的I / O线程? Or just a worker thread created to send the request to the device? 或者只是创建一个工作线程来将请求发送到设备?

Async IO is thread-less. Async IO是无线程的。 There is no thread being created or blocked. 没有创建或阻止线程。 That is the entire point of using async IO! 这是使用异步IO的全部意义! What would it help you to unblock one thread and block another? 什么会帮助您解锁一个线程并阻止另一个线程?

The internals of this have been discussed many times. 已经多次讨论了这种内部结构。 Basically, the OS notifies the CLR when the IO is done. 基本上,操作系统在IO完成时通知CLR。 This causes the CLR to queue the completion callback you specified onto the thread-pool. 这会导致CLR将您指定的完成回调排队到线程池中。

Short answer: You don't care. 简短的回答:你不在乎。

Long answer: 答案很长:

There is no thread. 没有线程。 More exactly, there is no thread for each of the asynchronous request you create. 更确切地说,您创建的每个异步请求都没有线程。 Instead, there's a bunch of I/O threads on the thread pool, which are mostly "waiting" on IOCP - waiting for the kernel to wake them up when data is available. 相反,线程池上有一堆I / O线程,它们主要在IOCP上“等待” - 等待内核在数据可用时将其唤醒。

The important point is that each of these can respond to any notification, they're not (necessarily) selective. 重要的一点是,每一个都可以响应任何通知,它们(必然)没有选择性。 If you need to process 1000 responses, you can still do so with just the one thread - and it is only at this point that a thread from the thread pool is requested; 如果你需要处理1000个响应,你仍然可以只使用一个线程 - 而且只是在此时才请求来自线程池的线程; it has to execute the callback method. 它必须执行回调方法。 For example, using synchronous methods, you'd have to keep a thousand threads to handle a thousand TCP connections. 例如,使用同步方法,您必须保留一千个线程来处理一千个TCP连接。

Using asynchronous methods, you only need the one IOCP thread, and you only have to spawn (or rather, borrow) new threads to execute the callbacks - which are only being executed after you get new data / connection / whatever, and are returned back to the thread pool as soon as you're done. 使用异步方法,您只需要一个IOCP线程,并且您只需要生成(或者更确切地说,借用)新线程来执行回调 - 这些回调仅在您获得新数据/连接之后执行,并且返回一旦你完成,就到线程池。 In practice, this means that a TCP server can handle thousands of simultaneous TCP connections with just a handful of threads. 实际上,这意味着TCP服务器只需少量线程即可处理数千个并发TCP连接。 After all, there's little point in spawning more threads than your CPU can handle (which these days is usually around twice as much as you've got cores), and all the things that don't require CPU (like asynchronous I/O) don't require you to spawn new threads . 毕竟,产生更多的线程比你的CPU可以处理的更少(这些天通常是你拥有内核的两倍),以及所有不需要CPU的东西(如异步I / O) 不要求你产生新的线程 If the handful of processing threads isn't enough, adding more will not help, unlike in the synchronous I/O scenario. 如果少数处理线程不够,那么与同步I / O场景不同,添加更多线程将无济于事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM