英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论