两个或更多线程如何在已分配的堆上共享内存? [英] How do two or more threads share memory on the heap that they have allocated?

查看:336
本文介绍了两个或更多线程如何在已分配的堆上共享内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所述,两个或多个线程如何在它们分配的堆上共享内存?我一直在考虑它,我想不通他们怎么做.这是我对过程的理解,大概是我在某个地方错了.

As the title says, how do two or more threads share memory on the heap that they have allocated? I've been thinking about it and I can't figure out how they can do it. Here is my understanding of the process, presumably I am wrong somewhere.

任何线程都可以通过进行系统调用来添加或删除堆上给定数量的字节,该系统调用返回指向该数据的指针,大概是通过写入寄存器,然后该线程可以将其复制到堆栈中. 因此,两个线程A和B可以根据需要分配尽可能多的内存.但是我看不到线程A如何知道线程B分配的内存位于何处.我也不知道任何一个线程如何知道另一个线程的堆栈位于何处.多线程程序共享堆,我相信可以访问彼此的堆栈,但我不知道怎么做.

Any thread can add or remove a given number of bytes on the heap by making a system call which returns a pointer to this data, presumably by writing to a register which the thread can then copy to the stack. So two threads A and B can allocate as much memory as they want. But I don't see how thread A could know where the memory that thread B has allocated is located. Nor do I know how either thread could know where the other thread's stack is located. Multi-threaded programs share the heap and, I believe, can access one another's stack but I can't figure out how.

我尝试搜索此问题,但只找到了抽象特定细节的特定语言版本.

I tried searching for this question but only found language specific versions that abstract away the details.

我尝试不特定于语言或操作系统,但是我正在使用Linux,并且从低角度来看它,我想是汇编.

I am trying not to be language or OS specific but I am using Linux and am looking at it from a low level perspective, assembly I guess.

推荐答案

我对您的问题的解释:线程A如何才能知道指向正在使用的内存B的指针?他们如何交换数据?

My interpretation of your question: How can thread A get to know a pointer to the memory B is using? How can they exchange data?

答案:它们通常从指向公共存储区的公共指针开始.这样一来,他们就可以彼此交换其他数据,包括指向其他​​数据的指针.

Answer: They usually start with a common pointer to a common memory area. That allows them to exchange other data including pointers to other data with each other.

示例:

  1. 主线程分配一些共享内存,并将其位置存储在p
  2. 主线程启动两个工作线程,将指针p传递给它们
  3. 工人现在可以使用p并处理p指向的数据
  1. Main thread allocates some shared memory and stores its location in p
  2. Main thread starts two worker threads, passing the pointer p to them
  3. The workers can now use p and work on the data pointed to by p

以一种真实的语言(C#)看起来像这样:

And in a real language (C#) it looks like this:

//start function ThreadProc and pass someData to it
new Thread(ThreadProc).Start(someData)

线程通常不访问彼此的堆栈.一切都从传递给线程过程的一个指针开始.

Threads usually do not access each others stack. Everything starts from one pointer passed to the thread procedure.

创建线程是OS的功能.它是这样的:

Creating a thread is an OS function. It works like this:

  1. 应用程序使用标准ABI/API调用操作系统
  2. 操作系统分配堆栈内存和内部数据结构
  3. 操作系统伪造"第一个堆栈帧:它将指令指针设置为ThreadProc并将someData推"到堆栈上.我之所以说伪造",是因为第一个堆栈帧不是自然产生的,而是由操作系统人为创建的.
  4. 操作系统调度线程. ThreadProc不知道它是在新堆栈上设置的.它所知道的只是someData处于期望的常规堆栈位置.

这就是someData到达ThreadProc的方式.这是共享第一个初始数据项的方式.步骤1-3由父线程同步执行. 4在子线程上发生.

And that is how someData arrives in ThreadProc. This is the way the first, initial data item is shared. Steps 1-3 are executed synchronously by the parent thread. 4 happens on the child thread.

这篇关于两个或更多线程如何在已分配的堆上共享内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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