在 C# 中,线程如何返回到与它来自的位置不同的位置?
我在 CLR via C# 一书中遇到了以下摘录:
Whenever you obtain a stack trace, you might find that some methods in the actual call stack don’t appear in the stack trace string. There are two reasons for this. First, the stack is really a record of where the thread should return to, not where the thread has come from. Second, the just-in-time (JIT) compiler can inline methods to avoid the overhead of calling and returning from a separate method.
摘录的意思是线程不能返回到它来的地方还是别的意思?在前一种情况下,有人可以举一个线程没有返回到它的来源的例子吗?在后一种情况下,有人可以简单地解释一下它的含义吗?
对我来说,这段摘录令人困惑,因为我已经习惯了函数中 return
语句的行为——它们总是返回调用者——线程来自的地方,我无法想象他们会回到另一个地方的情况。它与 async/await
语法糖有什么关系吗?
最佳答案
这段摘录指出了将返回地址压入堆栈的目的:它实际上是要返回的地址,而不是跳转起点的地址。
另一种看待它的方式是调用不需要与返回配对:您可以使用 push-jump-return
而不是 call-return
,这会将您带到您插入的位置,而不是您跳出的位置。
关于c# - 在 C# 中,线程如何返回到与它来自的位置不同的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60177939/