当添加新线程时的堆栈行为

huangapple go评论130阅读模式
英文:

Stack behavior when adding new thread

问题

当我们创建新线程时,堆栈会发生什么变化?
如果它进入堆栈顶部,那么它如何成为一个线程呢?如果它没有进入堆栈顶部,那么程序如何知道在哪里继续执行它的任务呢?

英文:

What happens to stack when we create new thread?
If it gets on the top of the stack, so how is it a thread?! If it doesn't get on the top of the thread, so how the program know where to continue it's job?

答案1

得分: 0

每个线程都有自己的堆栈。因此,如果初始线程创建了第二个线程,那么这不会影响第一个线程的堆栈。第二个线程将有它自己的堆栈。因此,在这个过程中存在两个独立的堆栈 - 每个线程一个。但是每个线程只会使用它自己的堆栈。操作系统会看到这两个线程,并在从一个线程切换到另一个线程时确保设置/使用适当的堆栈。

英文:

Each thread gets it's own stack. So if the initial thread creates a second thread, then this does not affect the stack of the first thread. And the second thread will have it's own stack. So in that process two independant stacks exist - one per thread. But each thread will only use it's own thread. The operating system sees both threads - and takes care that the proper stack is being set/used when switching from one thread to another.

huangapple
  • 本文由 发表于 2020年5月4日 02:43:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/61579813.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定