线程之间共享哪些资源? [英] What resources are shared between threads?

查看:794
本文介绍了线程之间共享哪些资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,在一次采访中有人问我一个问题,流程和线程之间有什么区别.真的,我不知道答案.我想了一会儿,给出了一个很奇怪的答案.

线程共享相同的内存,进程不共享.在回答了这个问题之后,面试官给了我一个邪恶的微笑,并向我开出了以下问题:

您知道程序划分为哪些部分吗?

我的回答:是的(这很简单)堆栈,数据,代码,堆

所以,告诉我:线程共享哪些段?

我无法回答这个问题,最后都说了全部.

请问,有人可以针对进程和线程之间的差异给出正确而令人印象深刻的答案吗?

解决方案

您是非常正确的,但是线程共享所有段(除外).线程具有独立的调用堆栈,但是其他线程堆栈中的内存仍然可以访问,并且从理论上讲,您可以在其他线程的本地堆栈帧中保留指向内存的指针(尽管您可能应该找到放置该内存的更好位置!). /p>

Recently, I have been asked a question in an interview what's the difference between a process and a thread. Really, I did not know the answer. I thought for a minute and gave a very weird answer.

Threads share the same memory, processes do not. After answering this, the interviewer gave me an evil smile and fired the following questions at me:

Q. Do you know the segments in which a program gets divided?

My answer: yep (thought it was an easy one) Stack, Data, Code, Heap

Q. So, tell me: which segments do threads share?

I could not answer this and ended up in saying all of them.

Please, can anybody present the correct and impressive answers for the difference between a process and a thread?

解决方案

You're pretty much correct, but threads share all segments except the stack. Threads have independent call stacks, however the memory in other thread stacks is still accessible and in theory you could hold a pointer to memory in some other thread's local stack frame (though you probably should find a better place to put that memory!).

这篇关于线程之间共享哪些资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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