我的问题不适用于C#和Asp.net,但我更容易提出更具体的问题。
当一个Asp.net请求等待一个异步IO操作时,该线程基本上转到线程池以供其他请求重用。 为什么在IO操作完成之前,这比直接睡眠线程更有效? 毕竟,当线程返回到线程池时,它的堆栈需要保存在内存中以完成原始请求。 我的假设是我们不能重新使用分配给线程的内存,除非我们在别处复制了使用的堆栈内存,并且复制数据可能会带来额外的开销,这可能是不合理的。
我错过了什么吗? 我的猜测是错误的吗? 请解释。
编辑:jlew指出的答案缺少一点。 当线程返回到池时,请求使用的堆栈内存会发生什么变化? 如果我们不能重用内存,那么重用线程有什么意义呢? 如果我们想重用未使用的堆栈部分,那么我们将不得不移动一些内存。 如果是这样,移动内存并重用未使用的堆栈内存可以提高整体效率吗?
My question is not applicable only to C# and Asp.net but it is easier for me to ask more specifically.
When an Asp.net request awaits an async IO operation, the thread basically goes to the thread pool to be reused by other requests. Why is this more efficient than just sleeping the thread until the IO operation is finished? After all, when the thread is returned to the thread pool, its stack needs to be kept in memory to finish the original request. My assumption is that we cannot reuse the memory allocated to the thread unless we copy used stack memory somewhere else and copying data may introduce additional overhead that may not be justified.
Am I missing something? Are my assumtions wrong? Please explain.
EDIT: The answer pointed by jlew is missing one point. What happens to the stack memory used by the request when the thread is returned to the pool? If we cannot reuse the memory, then what is the point of reusing the thread? If we want to reuse the part of the stack that is not used, then we will have to move some memory around. If so, does moving memory around and reusing unused stack memory improve overall efficiency?
更多推荐
发布评论