一个线程 return 怎么会去到一个不同于它在 C# 中的来源的地方呢?

How could a thread return to a place different from the one it come from in C#?

一个线程如何 return 到一个不同于它在 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不去它来的地方还是有别的意思?在前一种情况下,有人可以举一个线程不 return 到它来自哪里的例子吗?在后一种情况下,有人可以简单地解释一下它的含义吗?

对我来说,这段摘录令人困惑,因为我习惯了函数中 return 语句的行为 - 它们总是 return 到调用者 - 线程来自的地方,我无法想象他们会 return 到另一个地方的情况。它与 async/await 语法糖有什么关系吗?

这段摘录指出了将 return 地址压入堆栈的目的:它实际上是指向 return 的地址,而不是跳转起点的地址。

另一种看待它的方式是调用不需要与 returning 配对:您可以使用 push-jump-return 而不是 call-return,这会让您你推的位置,而不是你跳的位置。

让我们想想这个场景:

线程 A 启动线程 B。

线程 B 启动并在线程 C 中运行。

现在它 "returned" 到线程 C,尽管来自线程 A。