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

编程入门 行业动态 更新时间:2024-10-08 23:00:18
本文介绍了线程具有堆栈和进程具有堆栈的策略是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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.

更多推荐

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

本文发布于:2023-11-25 20:59:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631322.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:堆栈   线程   进程   策略

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!