异步编程,线程和效率(Async programming, threads and efficiency)

编程入门 行业动态 更新时间:2024-10-24 06:25:39
异步编程,线程和效率(Async programming, threads and efficiency)

我的问题不适用于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?

更多推荐

本文发布于:2023-08-07 02:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1458813.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   效率   Async   efficiency   threads

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!