线程具有堆栈和进程具有堆栈的策略是什么? [英] What's the policy for a thread to have a stack and a process to have a stack?

查看:118
本文介绍了线程具有堆栈和进程具有堆栈的策略是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

线程具有堆栈和进程具有堆栈的策略是什么.

What's the policy for a thread to have a stack and a process to have a stack.

如果我们有10个进程,那么我们有多少个堆栈10个?

If we have 10 process, how many stacks we have, 10?

如果在一个进程中有10个线程,那么我们有多少个堆栈,1个?所有线程共享同一个堆栈?

If we have 10 threads under one process, how many stacks we have, 1? All the thread shared the same stack?

谢谢!

推荐答案

如果您考虑堆栈是什么,则共享堆栈是没有意义的.

If you think about what the stack is, it doesn't make sense to share a stack.

请记住,这是一个调用堆栈,而不是数据结构.当您的指令指针(指示要执行的指令)在程序中移动时,它将遇到函数调用,这些函数调用将当前上下文(本地变量,调用前的IP)推入堆栈,然后跳转到被调用的函数.该函数将堆栈的顶部用作其局部变量,等等,完成后会弹出堆栈,将原始函数的局部变量放在顶部,然后将IP恢复到函数调用之后.

Remember that this is a call stack not the data structure. As your instruction pointer (indicating the instruction to execute) moves through your program, it will encounter function calls which push the current context (local variables, IP before call) onto the stack before jumping to the called function. That function uses the top of the stack for its local variables, etc and when it's done the stack gets popped leaving the local variables for your original function on the top, and restoring the IP to just after the function call.

如果两个线程具有相同的堆栈,则它们将共享上下文,但是可以想象它们具有不同的IP.如果其中一个线程调用了一个函数,则堆栈对于另一个线程(仍然在原始函数中)不再有意义.如果IP是同步的,那么您就不会有两个不同的线程-您只需做两次就可以了.

If two threads had the same stack, they would be sharing context, but they could conceivably have different IPs. If one of the threads called a function, the stack would no longer make sense for the other thread (which is still in the original function). If the IPs are synchronised, you haven't got two distinct threads - you're just doing everything twice.

正如其他人所说:每个线程,每个进程一个堆栈.

So as others have said: One stack per thread, per process.

这篇关于线程具有堆栈和进程具有堆栈的策略是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆